英文:
Java Graphics AWT
问题
以下是翻译好的代码部分:
import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;
public class gfix extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(80, 100, 150, 75);
}
public static void main(String[] args){
gfix gg = new gfix();
JFrame frame = new JFrame("RISK");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JButton button = new JButton("test");
button.setBounds(100, 100, 150, 150);
panel.add(button);
frame.setVisible(true);
}
}
英文:
When I run the below code, I am unable to see the background color as red. It's showing default one. Is there anything that I have to add to these lines?
import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;
public class gfix extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(80, 100, 150, 75);
}
public static void main(String[] args){
gfix gg=new gfix();
JFrame frame = new JFrame("RISK");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JButton button = new JButton("test");
button.setBounds(100, 100, 150, 150);
panel.add(button);
frame.setVisible(true);
}
}
答案1
得分: 1
你正在gfix
类中重写paintGraphics()
方法,所以将gfix
类对象添加到你的窗口中,而不是添加Java提供的JPanel
类对象。
gfix gg = new gfix();
JFrame frame = new JFrame("RISK");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JPanel panel = new JPanel(); 不需要这一行
// panel.setLayout(null);
frame.add(gg);
JButton button = new JButton("test");
button.setBounds(100, 100, 150, 150);
gg.add(button);
frame.setVisible(true);
对于g.fillRect(80, 100, 150, 75);
,请给定适当的面板边界以填充整个面板背景。或者在paintGraphics
中使用int width = getWidth(); int height = getHeight();
来获取实际的高度和宽度。
英文:
Your are overriding painGraphics()
in gfix
class so add gfix
class object into your frame not Java provided JPanel
class object.
gfix gg=new gfix();
JFrame frame = new JFrame("RISK");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel panel = new JPanel(); Not needed
//panel.setLayout(null);
frame.add(gg);
JButton button = new JButton("test");
button.setBounds(100, 100, 150, 150);
gg.add(button);
frame.setVisible(true);
for g.fillRect(80, 100, 150, 75);
give proper panel bounds to fill complete panel background. OR use int width = getWidth();
in paintGraphics to get actual height and width.
int height = getHeight();
答案2
得分: 0
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class gfix {
public static void main(String[] args) {
JFrame frame = new JFrame("RISK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
frame.add(panel);
JButton button = new JButton("test");
// adjust numbers as needed
button.setMargin(new Insets(20,40,20,40));
panel.add(button);
// adjust numbers as needed
panel.setBorder(new EmptyBorder(10,40,50,200));
frame.pack();
frame.setVisible(true);
}
}
其他提示:
- Java GUI 需要在不同的操作系统、屏幕尺寸、屏幕分辨率等条件下运行,使用不同的 PLAFs(平台相关的观感和感觉)。因此,它们不适合像素完美的布局。而应该使用布局管理器,或者结合布局填充和边框来创建空白间隙。
- 请提供 ASCII 艺术或简单绘图,以显示 GUI 的预期布局,包括最小尺寸,如果可调整大小,还应包括更大的宽度和高度,以显示额外的空间应如何使用。
- 请学习常见的 Java 命名约定(命名规范),例如
EachWordUpperCaseClass
、firstWordLowerCaseMethod()
、firstWordLowerCaseAttribute
,除非它是一个UPPER_CASE_CONSTANT
,并保持一致使用。
英文:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class gfix {
public static void main(String[] args) {
JFrame frame = new JFrame("RISK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
frame.add(panel);
JButton button = new JButton("test");
// adjust numbers as needed
button.setMargin(new Insets(20,40,20,40));
panel.add(button);
// adjust numbers as needed
panel.setBorder(new EmptyBorder(10,40,50,200));
frame.pack();
frame.setVisible(true);
}
}
Other tips:
- Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
- Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
- Please learn common Java nomenclature (naming conventions - e.g.
EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
unless it is anUPPER_CASE_CONSTANT
) and use it consistently.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论