2012-07-05 15 views
1

我对Java AWT很新颖。我的问题标题对你来说似乎很荒谬,对此感到遗憾。在我的应用程序中,我有三个按钮,点击时显示不同的线程。现在我想添加一个按钮或复选框或选择列表,当点击一个特定的按钮时。例如,如果我点击是按钮,它应该显示一个选择列表,类似的东西。我如何实现这样的目标?这是我到目前为止的代码:如何在JFrame中显示不同的组件?

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

Java awt是一个旧框架,没有得到高度支持。你真的应该考虑使用Swing或SWT – ControlAltDel 2012-07-05 16:03:31

+2

我第二次ControlAltDel,并建议你不使用AWT组件,而是使用Swing组件。 – 2012-07-05 16:26:42

+0

你是对的 - 我必须切换到Swing组件 – Saturnian 2012-07-06 14:36:00

回答

2

点描述你应该做的事情:

  • 正如别人已提到的,最好使用荡过AWT,Swing的,因为是比较先进的。
  • 尽可能,总是试图PaintJPanelJComponent的顶部,而不是Painting就在您的JFrame上,通过 覆盖的说 JComponent/JPanel
  • paintComponent(Graphics g)方法,一定不要调用setVisible(true)JFrame直到并且除非它的尺寸已经确定为 。因此,一般而言,这必须是最后一次调用 ,一旦您完成将组件添加到JFrame和 之后,LayoutManager就已实现了JFrame的大小。
  • 里面你actionPerformed(...),而不是写所有if statement blocks,你应该坚持if-else if statement blocks。这样做的好处,在前者的是,在任何给定时间 ,只有一个事件会被解雇,因此一旦上述条件满足 ,你不希望你的代码继续检查其他 条件,这在一般真的不是一个好的编程 练习,恕我直言。
  • 最重要的事情:永远不要像包()的调用/调用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); 
    } 
} 
+0

非常感谢 - 我将尽快切换到Swing组件! – Saturnian 2012-07-06 14:25:55

+0

你是最欢迎和保持微笑:-) – 2012-07-06 18:54:09

2

我不知道我是否已经明白这个问题很好,但......不能创建这些元素并调用它们的调用setVisible(布尔)方法,以使它们不可见首先,当用户按下按钮时,它们可以看到它们?