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

最佳答案

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


正如其他人已经提到的,最好在AWT上使用Swing,因为Swing更高级。
尽可能尝试将Paint放在JPanel
JComponent,而不是直接在Painting顶部的JFrame
覆盖所述的paintComponent(Graphics g)方法
JComponent/JPanel
除非且除非在setVisible(true)上呼叫JFrame
大小已确定。所以总的来说,这必须是
最后一次调用,一旦完成将组件添加到JFrame
JFrame的大小已由LayoutManager实现。
在您的actionPerformed(...)内部,而不是全部写ifstatement blocks,您应该遵守if-else if statementblocks。与前者相比,这样做的好处是
时间,只会触发一个事件,因此一旦上述情况发生
满意,您不希望您的代码继续检查其他
条件,这实际上不是一个好的编程
实践,恕我直言。
最重要的事情:切勿从main方法内进行类似pack()/ setVisible(...)的调用,此类调用属于
到事件调度线程,并且必须在同一线程上完成。请
阅读Concurrency in 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);
    }
}

10-07 19:18
查看更多