如何在JFrame中显示不同的组件?

时间:2012-07-05 16:00:46

标签: java swing jframe awt actionlistener

我是Java AWT的新手。我的问题标题对你来说似乎很荒谬,抱歉。在我的应用程序中,我有三个按钮,单击时显示不同的线程。现在,我想在单击特定按钮时添加按钮或复选框或选择列表等。例如,如果我点击“是”按钮,它应该显示一个选择列表,就像那样。我如何实现这样的目标?到目前为止,这是我的代码:

import java.awt.Button;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class AppWindow extends Frame implements ActionListener{
    String keymsg = "Test message";
    String mousemsg = "Nothing";
    int mouseX=30, mouseY=30;
    String msg;
    public AppWindow(){
        //addKeyListener(new MyKeyAdapter(this));
        //addMouseListener(new MyMouseAdapter(this));
        addWindowListener(new MyWindowAdapter());
    }

    public void paint(Graphics g){
        g.drawString(msg, 150, 100);
    }

    //Here the window is created:

    public static void main(String args[]){
        AppWindow appwin = new AppWindow();

        appwin.setSize(new Dimension(300,200));
        appwin.setTitle("My first AWT Application");
        appwin.setLayout(new FlowLayout(FlowLayout.LEFT));
        appwin.setVisible(true);

        Button yes,no,maybe;
        yes = new Button("yes");
        no = new Button("no");
        maybe = new Button("maybe");

        appwin.add(yes);
        appwin.add(no);
        appwin.add(maybe);

        yes.addActionListener(appwin);
        no.addActionListener(appwin);
        maybe.addActionListener(appwin);


    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        // TODO Auto-generated method stub
        String str = ae.getActionCommand();
        if(str.equals("yes")){
            msg = "You pressed Yes";
        }
        if(str.equals("no")){
            msg = "You pressed No";
        }
        if(str.equals("maybe")){
            msg = "You pressed Maybe";
        }

        repaint();
    }


}


class MyWindowAdapter extends WindowAdapter {
    public void windowClosing(WindowEvent we){
        System.exit(0);
    }
}

2 个答案:

答案 0 :(得分:2)

我不知道我是否已经理解了这个问题,但是......你不能创建这些元素并调用它们的setVisible(boolean)方法使它们最初不可见,并且它们使它们成为可能用户按下按钮时可见?

答案 1 :(得分:2)

描述你应该做什么的要点:

  • 正如其他人已经提到的,最好使用Swing over AWT,因为Swing更先进。
  • 尽可能在Paint或a JPanel之上尝试JComponent Painting代替JFrame而不是JComponent/JPanel,代替setVisible(true) 覆盖所述的paintComponent(Graphics g)方法 JFrame
  • 永远不要在JFrame上致电JFrame,除非是 规模已经确定。总的来说,这必须是 最后一次调用,一旦你完成了向actionPerformed(...)添加组件和 LayoutManager已实现if statement blocks的大小。
  • if-else if statement blocks内,您应该遵守import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ComponentExample { private CustomPanel drawingBoard; private JPanel contentPane; private JButton yesButton; private JButton noButton; private JButton maybeButton; private JComboBox cbox; private ActionListener buttonAction = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { JButton button = (JButton) ae.getSource(); if (cbox.isShowing()) contentPane.remove(cbox); if (button == yesButton) { drawingBoard.setText("You Pressed YES."); contentPane.add(cbox, BorderLayout.PAGE_END); } else if (button == noButton) drawingBoard.setText("You Pressed NO."); else if (button == maybeButton) drawingBoard.setText("You Pressed MAYBE."); /* * revalidate()/repaint() is needed * when the JComponent is added or * removed from the already * visible Container. */ contentPane.revalidate(); contentPane.repaint(); } }; public ComponentExample() { cbox = new JComboBox( new String[]{"I GOT IT" , "I STILL HAD DOUBT"}); } private void displayGUI() { JFrame frame = new JFrame("Component Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = new JPanel(); contentPane.setOpaque(true); contentPane.setBackground(Color.DARK_GRAY); contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(5, 5)); JPanel buttonPanel = new JPanel(); buttonPanel.setOpaque(true); buttonPanel.setBackground(Color.WHITE); yesButton = new JButton("YES"); yesButton.addActionListener(buttonAction); noButton = new JButton("NO"); noButton.addActionListener(buttonAction); maybeButton = new JButton("MAY BE"); maybeButton.addActionListener(buttonAction); buttonPanel.add(yesButton); buttonPanel.add(noButton); buttonPanel.add(maybeButton); contentPane.add(buttonPanel, BorderLayout.PAGE_START); drawingBoard = new CustomPanel(); contentPane.add(drawingBoard, BorderLayout.CENTER); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ComponentExample().displayGUI(); } }); } } class CustomPanel extends JPanel { private String msg; public CustomPanel() { msg = ""; setOpaque(true); setBackground(Color.WHITE); } public void setText(String msg) { this.msg = msg; repaint(); } @Override public Dimension getPreferredSize() { return (new Dimension(300, 300)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(msg, getWidth() / 3, getHeight() / 3); } } ,而不是全部{{1}}。对于前者而言,这一点的好处在于,在任何给定的情况下 时间,只有一个事件将被解雇,因此一旦上述条件是 满意,你不希望你的代码继续检查其他 条件,一般来说真的不是一个好的编程 惯例,恕我直言。
  • 最重要的事情:永远不要在main方法中调用pack()/ setVisible(...)这样的调用 到事件调度线程,必须在同一个上完成。请 请阅读Concurrency in Swing了解更多详情。

查看示例程序,以便更好地理解。

{{1}}