在我调整大小之前,组件不会显示在 Applet 中
我已经查看了这里的其他示例,但在创建所有组件后的 revalidate() 或 repaint() 。我也尝试过 this.setVisible(this);但这不起作用。我尝试在 createGUI() 方法中创建组件,并在 try/catch 语句中从 init() 方法运行该组件。
编辑 我尝试了你所有的例子,正如你在评论中看到的那样。但从大家的说法来看,为什么现在这样行得通呢?
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();
}
}
感谢您的帮助!
I've looked at the other examples on here but the revalidate() or repaint() after I create all of my components. I've also tried the this.setVisible(this); and that didn't work. I've tried creating my components in a createGUI() method and running that from the init() method within a try/catch statement.
EDIT
I tried all of your examples, as you can see in the comments. But from what everyone has said why does this now work?
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();
}
}
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原始代码的主要问题是您覆盖了paint()方法而不调用super.paint(g)。当您将该方法更改为paintComponent() 时,代码可以工作,因为该方法甚至不在Applet 中存在,因此它是死代码。
您的代码存在的问题:
@Override
注释,以确保正确重写该方法。首先阅读Swing 教程以获得更好的解释和工作示例。从以下部分开始:
The main problem with your original code was that your overrode the paint() method without invoking super.paint(g). When you changed that method to paintComponent() the code worked because that method doesn't even exits in an Applet so it was dead code.
The problems with your code:
@Override
annontation before the method signature to make sure you override the method correctly.Start by reading the Swing tutorial for a better explanation and working example. Start with the sections on:
您应该在 TotalGUI 上调用 repaint()。
调整大小后 gui 刷新的原因是调整大小会自动为您调用 repaint() 。但是,如果您希望 gui 更改立即出现,您应该调用 repaint();
然而,首选方法是在您的totalGUI的paint(Graphics g)/paintComponent(Graphics g)方法中使用:
如这些帖子所述:
JPanel 重绘问题
http://www.sitepoint.com/forums/showthread.php?273522-super.paintComponent()
You should call repaint() on TotalGUI.
The reason your gui refreshes after resize, is that resize automatically calls repaint() for you. However, if you want you gui changes to appear instantly, you should call repaint();
A preferred approach, however, is to use in your totalGUI's paint(Graphics g)/paintComponent(Graphics g) method/s:
as described by these posts:
JPanel repaint issue
http://www.sitepoint.com/forums/showthread.php?273522-super.paintComponent()