简体   繁体   English

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

[英]GUI programming, FlowLayout blocking other things on JFrame(?)

The idea of the program is that I have some buttons and an icon SOMEWHERE on the frame. 该程序的想法是我在框架上有一些按钮和一个图标SOMEWHERE。 I want the buttons to change the color. 我希望按钮改变颜色。 I'm only worried about making all the elements show up right now. 我只担心现在所有的元素都出现了。 If I comment out lines 11-13, I see "hello," printed out, with a red circle on top of it. 如果我评论第11-13行,我会看到“你好”,打印出来,上面有一个红色圆圈。 Otherwise, I just have the button "red" without "hello" or my red circle. 否则,我只有“红色”按钮没有“你好”或我的红色圆圈。 So here's my code: 所以这是我的代码:

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);
    } 
} 

I'm 90% sure the problem is lines 11-13, but I'm not sure what to change to make everything visible. 我90%肯定问题是第11-13行,但我不确定要改变什么以使一切都可见。

Your problem is that your ButtonPanel's size is 0. Have it override getPreferredSize() and you will see what I mean: 你的问题是你的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);
   }
}

Also as an unrelated aside, why are you creating an Icon inside of the paintComponent method? 另外,作为一个无关的,为什么要在paintComponent方法中创建一个Icon? This doesn't make sense to me and would only serve to needlessly slow your graphics down. 这对我来说没有意义,只会不必要地减慢你的图形速度。

Edit 编辑
You state: 你说:

Ok, I see the difference after overriding getPreferredSize() But what would be the "better" or "correct" way to create the icon? 好的,我在重写getPreferredSize()之后看到了区别但是创建图标的“更好”或“更正”的方法是什么? I'm just trying to follow the directions for an exercise out of a Java textbook: Exercise 4.14. 我只是想按照Java教科书的练习指示:练习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. 对重绘的调用确保调用paintIcon方法,以便可以使用新颜色重新绘制图标。

You need to think on this a different way. 你需要以不同的方式思考这个问题。 Myself I'd create three ImageIcons one for a blue circle, one for red, and one for green. 我自己创建了三个ImageIcons,一个用于蓝色圆圈,一个用于红色,一个用于绿色。 I'd then display the ImageIcon in a JLabel on my JFrame. 然后我会在JFrame上的JLabel中显示ImageIcon。 I'd change the color by simply swapping the label's icons via its setIcon(...) method. 我只需通过setIcon(...)方法交换标签的图标就可以改变颜色。 I wouldn't worry about futzing with paintComponent(...) but rather would try to solve this in as simple a fashion as possible. 我不会担心使用paintComponent(...)预测,而是尝试以尽可能简单的方式解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM