1

我对 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);
    }
}
4

2 回答 2

2

我不知道我是否很好地理解了这个问题,但是......你不能创建这些元素并调用它们的 setVisible(boolean) 方法以使它们一开始不可见,并且当用户按下按钮时它们使它们可见?

于 2012-07-05T16:08:49.993 回答
2

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

  • 正如其他人已经提到的,最好使用 Swing 而不是 AWT,因为 Swing 更高级。
  • 尽可能地,总是尝试Paint在 aJPanel或 a 之上JComponent,而不是Painting在你的之上JFrame,通过覆盖上述的paintComponent(Graphics g)方法 JComponent/JPanel
  • 永远不要调用直到,除非它setVisible(true)JFrame大小已经确定。所以一般来说,这必须是最后一次调用,一旦你完成向 LayoutManager 添加组件JFrame并且 LayoutManager 的大小JFrame已经实现。
  • 在你的里面actionPerformed(...),而不是写所有if statement blocks,你应该坚持if-else if statement blocks。与前者相比,这样做的好处是,在任何给定时间,只会触发一个事件,因此一旦满足上述条件,您不希望您的代码继续检查其他条件,这通常不是一个很好的编程习惯,恕我直言。
  • 最重要的事情:永远不要在 main 方法中进行像 pack()/setVisible(...) 这样的调用,这样的调用属于 Event Dispatch Thread,并且必须在同一个方法上进行。有关更多详细信息,请阅读Swing 中的并发。

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

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);
    }
}
于 2012-07-05T18:14:20.620 回答