在我调整大小之前,组件不会显示在 Applet 中

发布于 2024-12-25 10:02:06 字数 2238 浏览 2 评论 0原文

我已经查看了这里的其他示例,但在创建所有组件后的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2025-01-01 10:02:06

原始代码的主要问题是您覆盖了paint()方法而不调用super.paint(g)。当您将该方法更改为paintComponent() 时,代码可以工作,因为该方法甚至不在Applet 中存在,因此它是死代码。

您的代码存在的问题:

  1. 您应该为 Swing applet 扩展 JApplet
  2. 您不应该重写 Applet 的 Paint() (或 PaintComponent())方法。如果您需要进行自定义绘制,那么您可以重写 JPanel(或 JComponent)的 PaintComponent() 方法,并将面板添加到小程序的内容窗格中。
  3. 该代码应在 EDT 上执行。
  4. 小程序会自动显示组件,无需调用重绘)
  5. 切勿使用 getGraphics() 进行自定义绘制。使用paintComponent() 方法的Graphics 对象。
  6. 当您尝试重写方法时,请不要忘记在方法签名之前使用 @Override 注释,以确保正确重写该方法。

首先阅读Swing 教程以获得更好的解释和工作示例。从以下部分开始:

  1. 如何使小程序
  2. 执行自定义绘画

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:

  1. You should be extending JApplet for a Swing applet
  2. You should NOT override the paint() (or paintComponent()) method of an Applet. If you need to do custom painting then you override the paintComponent() method of a JPanel (or JComponent) and add the panel to the content pane of the applet.
  3. The code should be executed on the EDT.
  4. The applet will display the components automatically, there is no need to invoke repaint)
  5. Never use getGraphics() to do custom painting. Use the Graphics object of the paintComponent() method.
  6. When you attempt to override a method don't forget to use the @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:

  1. How to Make Applets
  2. Performing Custom Painting
时光磨忆 2025-01-01 10:02:06

您应该在 TotalGUI 上调用 repaint()。

调整大小后 gui 刷新的原因是调整大小会自动为您调用 repaint() 。但是,如果您希望 gui 更改立即出现,您应该调用 repaint();

然而,首选方法是在您的totalGUI的paint(Graphics g)/paintComponent(Graphics g)方法中使用:

super.paintComponent(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:

super.paintComponent(g);

as described by these posts:

JPanel repaint issue

http://www.sitepoint.com/forums/showthread.php?273522-super.paintComponent()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文