开发者

Java GUI - JPanels, JFrames, JButtons

I'm trying to open a window that has both an image and buttons in it. But I can't seem to figure out how to add the button. The image displays great and the menu works fine, but no matter where I add the button (into the JLabel, JPanel, or JFrame), it doesn't ever show...

Main:

public static void main(String[] args) {
    GUI myGUI = new GUI();
    myGUI.show();
}

GUI class: openImage is called when using the menu. The image then displays, but no button.

private JFrame myFrame;
private JPanel myPanel;
private JLabel myLabel;
public GUI()
{
    myFrame = new JFrame();
    initializePanel();
}

public void show()
{
    myFrame.setSize(600,600);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.addMouseListener(this);

    setupMenu(myFrame);     

    myFrame.setVisible(true);
}
private void initializePanel() 
{
       myPanel = new JPanel();
       myPanel.setPreferredSize(new Dimension(500,500));
       //myPanel.setLayout(new BorderLayout());
}
private void openImage(String fileName)
{
    try {
        myImage = ImageIO.read(new File(fileName));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }    

    myLabel = getJLabel();

    JButton button = new JButton("ButtonClick");
        button.addActionListener(this);

    myFrame.setContentPane(myLabel);

    myPanel.add(button);
    myFrame.getContentPane().add(myPanel);

    myFrame.pack();
    myFrame.setSize(600,600);
}
p开发者_StackOverflowrivate void setupMenu(JFrame window) {
        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenuItem open = new JMenuItem("Open");
        open.addActionListener(this);
        file.add(open);
        menubar.add(file);
        window.setJMenuBar(menubar);
}


Your main issue is your setting the contentPane to be a JLabel -- don't do this! The contentPane needs to be opaque, needs to be built to be easily used as a Container and in your case, really should be a JPanel. JLabel I believe uses a null layout so it's no surprise that your code shows no button. If you want to show a background image, make have myPanel constructed from an anonymous class that extends JPanel, override the paintComponent method in this class (calling super.paintComonent first in the method), and draw the image in this method. Then you can add components to the contentPane which will now use a FlowLayout (the default for a JPanel) and it will be opaque by default.

Also, if your goal is to swap items displayed in your GUI, use a CardLayout to do the swapping for you as this layout makes swapping components a breeze.


really don't know, depends of method(s) how you are added picture to the JLabel, JPanel, or JFrame, but maybe for simle Container that contains a few, only one-two JComponents is there crazy idea, without side effects, with idea to display picture and to add there JButton:

JLabel is very similair JComponent to the JPanel, and is by default translucent and very simple implements Icon/ImageIcon, then you'll only to call myLabel.setIcon(myPicture)

to the all of JComponents you are/could be able to add another JComponent by using some of LayoutManager (Box, Flow, GridBagLayout)


You tried to set the label as the content pane and then tried to add the panel to that image which doesn't make sanse at all.

Change it so you add the label to the panel and have the panel as content pane:

Like this:

Java GUI - JPanels, JFrames, JButtons


You have this line which is the problem. It doesn't make much sense:

myFrame.setContentPane(myLabel);

Try instead:

myFrame.getContentPane().add(myLabel); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜