paintComponent无法使用java,
javaswing,paintComponent 在下面的代码中,它正在执行,但函数paintComponent不起作用,那么这是什么原因呢。我是java新手,所以请犯这些错误paintComponent无法使用java,,java,swing,paintcomponent,Java,Swing,Paintcomponent,javaswing,paintComponent 在下面的代码中,它正在执行,但函数paintComponent不起作用,那么这是什么原因呢。我是java新手,所以请犯这些错误 package practice; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class gui extends JPanel implements ActionListener
package practice;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class gui extends JPanel implements ActionListener
{
JFrame frame;
JButton button;
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
}
public void go()
{
frame=new JFrame();
button=new JButton("click me");
frame.getContentPane().add(button);
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(2000,2000);
frame.setVisible(true);
}
public static void main(String[] args)
{
gui g=new gui();
g.go();
}
public void actionPerformed(ActionEvent e)
{
button.setText("mmmmm");
}
}
您从未将gui实例添加到JFrame,因此不会显示JPanel。您在main方法中创建了一个
newgui()
对象,但从未将此对象添加到JFrame中。一个等价的位是将这个添加到go()
方法中的JFrame中。您还需要将JButton添加到this
,而不是JFrame。必须将JPanel添加到顶级窗口中,才能使其可视化。它不会神奇地出现
例如:
public void go()
{
frame=new JFrame();
button=new JButton("click me");
// frame.getContentPane().add(button); // !! Nope
this.add(button); // !! add button to the JPanel
frame.getContentPane().add(this); // !! add the JPanel to the JFrame
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(2000,2000);
frame.setVisible(true);
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Gui extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private JButton button = new JButton(new ButtonAction("Click Me"));
public Gui() {
add(button);
setBackground(Color.BLACK); // now no need to override paint/paintComponent
}
@Override
// to make GUI bigger
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// @Override // no longer needed
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
// g.setColor(Color.black);
// g.fillRect(0, 0, getWidth(), getHeight());
// }
// better than having the GUI class implement ActionListener:
private class ButtonAction extends AbstractAction {
public ButtonAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent arg0) {
putValue(NAME, "mmmmmmm");
}
}
private static void createAndShowGui() {
Gui mainPanel = new Gui();
JFrame frame = new JFrame("Gui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
此外,您应该覆盖paintComponent,而不是paint。尽管如此,要将JPanel的背景涂成黑色,您只需在设置JPanel的任何位置调用setBackground(Color.black)
:例如:
public void go()
{
frame=new JFrame();
button=new JButton("click me");
// frame.getContentPane().add(button); // !! Nope
this.add(button); // !! add button to the JPanel
frame.getContentPane().add(this); // !! add the JPanel to the JFrame
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(2000,2000);
frame.setVisible(true);
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Gui extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private JButton button = new JButton(new ButtonAction("Click Me"));
public Gui() {
add(button);
setBackground(Color.BLACK); // now no need to override paint/paintComponent
}
@Override
// to make GUI bigger
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
// @Override // no longer needed
// protected void paintComponent(Graphics g) {
// super.paintComponent(g);
// g.setColor(Color.black);
// g.fillRect(0, 0, getWidth(), getHeight());
// }
// better than having the GUI class implement ActionListener:
private class ButtonAction extends AbstractAction {
public ButtonAction(String name) {
super(name);
}
@Override
public void actionPerformed(ActionEvent arg0) {
putValue(NAME, "mmmmmmm");
}
}
private static void createAndShowGui() {
Gui mainPanel = new Gui();
JFrame frame = new JFrame("Gui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
另外,作为附带建议,为了帮助我们现在和将来,为了帮助您自己,请编辑您的代码并更改您的变量名,以符合Java命名约定:所有类名都以大写字母开头,方法/变量名以小写字母开头。此外,大多数字段都应该是私有的 我也试过使用paintComponent,但它不起作用。他们的调用或任何其他方法中有任何错误吗?“您从不向JFrame添加gui实例,因此不会显示JPanel。此外,您应该覆盖paintComponent,而不是paint。”您可以简单地解释一下这些行吗?@jatin5676:就像我说的:您在main方法中创建一个新gui()
对象,但您从未将此对象添加到JFrame。必须将JPanel添加到顶级窗口中,才能使其可视化。它不会神奇地出现。请参阅“编辑以回答”以查看更好的代码。@jatin5676:等效位是在go()
方法中将this
添加到JFrame中。您还需要将JButton添加到this
,而不是JFrame。i、 e.,this.add(按钮)代码>和帧。添加(此)代码>@jatin5676:请参见编辑中的其他信息以回答问题。