如何在框架中显示带有组件的面板

Posted

技术标签:

【中文标题】如何在框架中显示带有组件的面板【英文标题】:How to display panels with component in frame 【发布时间】:2011-02-25 09:06:49 【问题描述】:

为什么我的 JFrame 'frame' 显示空窗口,它应该给我 3 个菜单按钮和我自己绘制的 JComponent 下面?我在这里错过了什么?

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

public class Eyes extends JFrame 

    public static void main(String[] args) 
        final JFrame frame = new JFrame("Eyes");
        frame.setPreferredSize(new Dimension(450, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel players = new JPanel(new GridLayout(1, 3));
                players.add(new JButton("Eyes color"));
                players.add(new JButton("Eye pupil"));
                players.add(new JButton("Background color"));

        JPanel eyes = new JPanel();
        eyes.add(new MyComponent());

        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        content.add(players);
        content.add(eyes);

        frame.getContentPane();
        frame.pack();
        frame.setVisible(true);
    


class MyComponent extends JComponent 

    public MyComponent()

    

    @Override
    public void paint(Graphics g) 
        int height = 120;
        int width = 120;  
        Graphics2D g2d = (Graphics2D) g;
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        BasicStroke bs = new BasicStroke(3.0f);
        g2d.setStroke(bs);
        g2d.setColor(Color.yellow);
        g2d.fillOval(200, 200, height, width);
        g2d.setColor(Color.black);
        g2d.drawOval(60, 60, height, width);   
    

【问题讨论】:

自定义绘画应该通过覆盖paintComponent()方法而不是paint()方法来完成。你应该调用 super.paintComponent() 作为方法的第一条语句。 【参考方案1】:

您的线路:

    frame.getContentPane();

除了访问框架的内容窗格之外什么都不做。您应该设置内容窗格,而不是获取内容窗格,如下所示:

    frame.setContentPane(content);

编辑:

或者,正如@trashgod 指出的那样,您可以使用getContentPane 方法访问默认内容窗格并将content 组件添加到其中:

    frame.getContentPane().add(content);

【讨论】:

【参考方案2】:

我认为您正在尝试使用嵌套的JPanels。这当然是组织组件的一种方式,但缺点是在某些情况下难以管理。你可以试试下面这个sn-p的代码。在程序中你会发现:

1) JLabel的数组

2) JTextField的数组

3) 嵌套JPanels

在程序结束时,我使用Container 将这些对象的最终产品添加到我的图形窗口中。

我能想到的最有效的方法是在我的程序顶部定义这些组件,以便我以后可以根据需要重用它们。

要实现这一点,您可以尝试以下 sn-p 代码:

import javax.swing.*; //Required to use Swing Components

public class TestGUI extends JFrame

    JLabel[] label;         //Define this with an array
    JTextField[] textField; //Define this with an array as well

    private int nLabels;     //Number of labels preferred
    private int nTextFields; //Number of text fields preferred

    public testGUI(int amt)
    
        //Assuming that you want equal amounts of each,
        //set these two variables to the "ant" input parameter
        nLabels     = amt;
        nTextFields = amt;

        //Set component attributes
        label     = new JLabel[2];     //Label compared text fields
        textField = new JTextField[2]; //Use two of these for comparison

        textField[0].setEnabled(false); //Disabled editing
        //Do nothing with the second text field

        JPanel labels = new JPanel(); //Place JLabels here

        //Use this to align the labels vertically
        labels.setLayout(new GridLayout(2, 1));

        //Use this for loop to add the labels to this JPanel
        for(int i = 0; i < nLabels; i++)
        
            labels.add(label[i]);
            //You can also define and apply additional properties
            //to labels inside this loop. TIP: You can do this in
            //any loop
        

        JPanel txtFields = new JPanel(); //Place JTextFields here

        //Use this to align the text fields vertically
        txtFields.setLayout(new GridLayout(2, 1));

        //Use this for loop to add the labels to this JPanel
        for(int i = 0; i < nTextFields; i++)
        
            textFields.add(textField[i]);
            //You can also define and apply additional properties
            //to text fields inside this loop. TIP: You can do
            //this in any loop
        

        //Now we have the two components, you asked for help with, set up
        //Next, we will need another JPanel to add these to panels to.
        //This JPanel will be added to the JFrame Container
        //You probably know how to run this via the "main" method

        JPanel window = new JPanel();
        //Place the JPanel for the labels and text fields

        //This requires a horizontal grid
        window.setLayout(new GridLayout(1, 2));

        //Add the the two JPanels: "labels" and "txtFields"
        window.add(labels);
        window.add(txtFields);

        //Define the Container object to set up the GUI
        Container container = getContentPane();

        //Apply the "window" JPanel object to the container
        container.add(window, BorderLayout.CENTER);
        //Center this in the Graphics Window when displayed
    
    //Any other methods and/or functions can be added as well
    //If they are, they must placed in the constructor method above

这是我在尝试制作和操作我编写的图形窗口时使用的方法。有时我会编写小程序,但前提是要确保我在一个普通的图形窗口中一切正常。

我希望这会有所帮助。

如果您有任何其他问题,请告诉我,我会尽我所能回答,谢谢。

【讨论】:

以上是关于如何在框架中显示带有组件的面板的主要内容,如果未能解决你的问题,请参考以下文章

如何将多个带有标签的面板添加到单个框架中?

是否有可能制作一个类似面板的 React 组件,显示带有行号和折叠功能的源代码?

如何在运行时动态更改组件模板?角 4

如何在 ExtJS 中替换组件

如何在带有检票口的应用程序中使用一些动态反馈面板?

在半透明框架/面板/组件上重新绘制。