开发者

How to display panels with component in frame

Why my JFrame 'frame' is d开发者_运维百科iplaying empty window, when it should give me 3 menu buttons and my own painted JComponent below ? What am I missing here ?

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


Your line:

    frame.getContentPane();

doesnt do anything but access the content pane of the frame. Instead of getting the content pane, you should set your content pane, like this:

    frame.setContentPane(content);

EDIT:

alternatively, as @trashgod points out, you could use the getContentPane method to access the default content pane and add your content component to that:

    frame.getContentPane().add(content);


I think you are attempting to use nested JPanels. This is certainly a way to organize your components, but downside to is the fact that it gets difficult to manage in some cases. You could try this snippet of code below. In the program you will find:

1) An array of JLabel

2) An array of JTextField

3) Nested JPanels

At the end of the program I use the Container to add the final product of these object to my Graphics Window.

The most efficient way I can think of is to define these components at the top of my program so that I can reuse them later as I need to.

To achieve this you can try this snippet of code:

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
}

This is the approach that I would use when trying to go at making and manipulating my Graphics Windows that I write. Sometimes I write applets, but only after making sure that I have everything functioning properly in a plain Graphics Window.

I hope this helps.

If you have any other questions, just let me know and I will answer the to the best of my ability, thanks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜