java - GUI 编程,FlowLayout 阻塞了 JFrame 上的其他东西(?)

标签 java swing user-interface flowlayout preferredsize

这个程序的想法是我在框架的某个地方有一些按钮和一个图标。我想让按钮改变颜色。我只担心让所有元素现在都显示出来。如果我注释掉第 11-13 行,我会看到“hello”打印出来,上面有一个红色圆圈。否则,我只有按钮“红色”而没有“你好”或我的红色圆圈。所以这是我的代码:

import javax.swing.*; 
import java.awt.*;
import java.awt.geom.*;

public class ButtonTester 

{
    public static void main (String[] args) 
    {  
        JFrame frame = new ButtonFrame(); 
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton redButton = new JButton("Red");
        frame.add(redButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true); 
    }
}

class ButtonFrame extends JFrame 
{  
    public static final int DEFAULT_WIDTH = 300;  
    public static final int DEFAULT_HEIGHT = 200;  

    public ButtonFrame() 
    {   
        setTitle("Hello"); 
       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
       ButtonPanel panel = new ButtonPanel(); 
       add(panel); 
    } 
} 

class ButtonPanel extends JPanel 
{ 
    public void paintComponent(Graphics g) 
    {   
        super.paintComponent(g); 
        Graphics2D g2 = (Graphics2D) g;  
        g2.drawString("Hello !", 100, 100);
        Icon ico = new ColorIcon(32);
        ico.paintIcon(null, g, 75, 75);
    } 
} 

我 90% 确定问题出在第 11-13 行,但我不确定要更改什么才能使所有内容可见。

最佳答案

您的问题是您的 ButtonPanel 的大小为 0。让它覆盖 getPreferredSize() 并且您会明白我的意思:

class ButtonPanel extends JPanel {
   private static final int PREF_W = 150;
   private static final int PREF_H = PREF_W;

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.drawString("Hello !", 100, 100);
      // !! Icon ico = new ColorIcon(32);
      // Icon ico = new ImageIcon();
      // ico.paintIcon(null, g, 75, 75);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }
}

还有一个不相关的问题,你为什么要在 paintComponent 方法中创建一个图标?这对我来说没有意义,只会不必要地降低图形速度。

编辑
你声明:

Ok, I see the difference after overriding getPreferredSize() But what would be the "better" or "correct" way to create the icon? I'm just trying to follow the directions for an exercise out of a Java textbook: Exercise 4.14. Write a program that shows a frame with three buttons labeled "Red", "Green", and "Blue", and a label containing an icon showing a circle that is initially red. As the user clicks the buttons, the fill color of the circle should change. When you change the color, you need to invoke the repaint method on the label. The call to repaint ensures that the paintIcon method is called so that the icon can be repainted with the new color.

您需要以不同的方式思考这个问题。我自己会创建三个 ImageIcons,一个用于蓝色圆圈,一个用于红色,一个用于绿色。然后我会在我的 JFrame 上的 JLabel 中显示 ImageIcon。我会通过 setIcon(...) 方法简单地交换标签的图标来更改颜色。我不会担心 paintComponent(...) 的问题,而是会尝试以尽可能简单的方式解决这个问题。

关于java - GUI 编程,FlowLayout 阻塞了 JFrame 上的其他东西(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15181120/

相关文章:

java - Firebase 实时数据库不更新子项

Java-如何通过 GUI 获取数字数组

android - 启动屏幕应用程序和隐藏操作栏

user-interface - BlackBerry - 在位图字段上调用点击事件

java - 以二进制或字节码写入文件

java - 使 Java 中的 Scanner 对象的西里尔文可读

java - android - 如何在 xml 文件中搜索

java - 专注于Keylistener

java - JTextArea 在 Swing 中被切断

java - 如何修改 JTextField 以屏蔽字符而不使用 JPasswordField 因此