Java 组件不是';在我调整大小之前,不会在小程序中显示
我已经看了这里的其他示例,但是在创建所有组件之后,重新验证()或重新绘制()。我还尝试了this.setVisible(this);但那没用。我尝试在createGUI()方法中创建组件,并在try/catch语句中从init()方法运行该组件 编辑 我尝试了你所有的例子,你可以在评论中看到。但从每个人都说过的话来看,这为什么现在起作用Java 组件不是';在我调整大小之前,不会在小程序中显示,java,swing,applet,components,Java,Swing,Applet,Components,我已经看了这里的其他示例,但是在创建所有组件之后,重新验证()或重新绘制()。我还尝试了this.setVisible(this);但那没用。我尝试在createGUI()方法中创建组件,并在try/catch语句中从init()方法运行该组件 编辑 我尝试了你所有的例子,你可以在评论中看到。但从每个人都说过的话来看,这为什么现在起作用 package basic; import java.awt.*; //import java.applet.*; import java.applet.Ap
package basic;
import java.awt.*;
//import java.applet.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;
public class Shapes extends Applet implements ActionListener
{
JButton rectBtn = new JButton("Rectangle");
JButton circBtn = new JButton("Circle");
JLabel rectLbl = new JLabel("Rectangle"), circLbl = new JLabel("Circle");
JLabel widthLbl = new JLabel("Width"), heightLbl = new JLabel("Height");
JTextField widthTF = new JTextField(6), heightTF = new JTextField(6), colorTF;
boolean rectOn;
boolean circOn;
int x,y, width, height;
String xcord, ycord, widthSize, heightSize;
public void init()
{
JPanel TotalGUI = new JPanel(new GridLayout(2,0));
TotalGUI.add(rectLbl); TotalGUI.add(rectBtn);
rectBtn.addActionListener(this);
TotalGUI.add(circLbl); TotalGUI.add(circBtn);
circBtn.addActionListener(this);
TotalGUI.add(widthLbl); TotalGUI.add(widthTF);
TotalGUI.add(heightLbl); TotalGUI.add(heightTF);
add(TotalGUI, BorderLayout.WEST);
//this.setVisible(true);
TotalGUI.repaint();
//pack();
}
//@Override
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
//Graphics g2 = getGraphics();
if(rectOn)//if Rectangle has been pressed
{
g.drawRect(x,y, width,height);
}
if(circOn)//if Circle has been pressed
{
g.drawOval(x,y, width, height);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == rectBtn)
{
rectOn = true;
}
if(e.getSource() == circBtn)
{
circOn = true;
}
//Reads coordinates and sizes as Strings and converts to integers
try{
widthSize = widthTF.getText();
width = Integer.parseInt(widthSize);
heightSize = heightTF.getText();
height = Integer.parseInt(heightSize);
}
catch(Exception err) { JOptionPane.showMessageDialog(null, "Enter a number!"); }
repaint();
}
}
谢谢你的帮助 您应该在TotalGUI上调用repaint()
调整大小后gui会刷新的原因是,resize会自动为您调用repaint()。但是,如果希望gui更改立即显示,则应调用repaint()
但是,首选方法是在totalGUI的绘制(图形g)/绘制组件(图形g)方法中使用:
super.paintComponent(g);
如这些员额所述:
原始代码的主要问题是您在没有调用super.paint(g)的情况下重写了paint()方法。当您将该方法更改为paintComponent()时,代码起作用,因为该方法甚至不存在于小程序中,所以它是死代码 代码中的问题:
@override
注释,以确保正确重写方法也许我把repaint()放错地方了,因为它仍然不起作用。我把它称为init方法中的最后一件事。我将尝试super.paintComponent(g)方法,看看效果如何,谢谢:)-1无需调用repaint()。小程序将执行此操作。您也永远不会重写小程序的paintComponent()方法,因为该方法不存在。@camickr TotalGUI是JPanel,而不是小程序。JPanel是一个组件,因此具有paintComponent方法。此处发布的代码完全包含在扩展小程序类中。没有理由重写TotalGUI类的paintComponent()方法,因为代码只是向其添加组件。我现在明白你的意思了,但是建议不清楚。从更新的代码中可以看到,他刚刚将paint()方法更改为paintComponent()方法。另外,您不应该建议重写paint()方法,因为自定义绘制是在JPanel的paintComponent()方法中完成的。但在此之前,请删除
Graphics g=getGraphics()编码>并将对g
的所有引用替换为g2
。您还不会看到组件,但它可能会停止闪烁;不幸的是,这对闪烁没有帮助。1->Correct 2->Correct。没有人提出其他建议:)5->是。6->不相关,但validI总是查找Applet vs JApplet和一些论坛,它确实建议使用Applet,尽管这是旧方法。我把我的程序改成使用JApplet,所有的东西都消失了,所以我选择使用Applet。数字3是什么意思,在EDT上执行?建议将小程序与Swing组件一起使用的论坛是错误的!如果您的代码与JApplet不兼容,那么您的代码无效。这就是为什么我列出了代码中所有潜在的问题。EDT代表事件分派线程,在“并发”一节中有描述。有关如何使用EDT的适当示例也在“如何制作小程序”的教程中给出,其中代码使用invokeAndWait
。