View Single Post
  #2 (permalink)  
Old 03-30-2008, 09:35 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
// change this this.addActionListener(this); // to this mainButton.addActionListener(this);
Code:
import java.awt.*; import java.awt.event.*; public class GraphicTestRx extends Button implements ActionListener { private Frame mainScreen; private List mainList; private Button mainButton; private Dialog dialogBox; public void actionPerformed(ActionEvent actEvent){ String pressedButton = actEvent.getActionCommand(); if (pressedButton.equals("Press Me")) { dialogBox.setVisible(true); } } public void load() { mainScreen = new Frame("Main Window"); mainScreen.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); mainList = new List(); mainButton = new Button("Press Me"); dialogBox = new Dialog(mainScreen, "Dialog", true); dialogBox.setSize(200,200); dialogBox.setLocation(200,200); dialogBox.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { ((Dialog)e.getSource()).dispose(); } }); mainList.add("A", 0); mainList.add("List", 1); // mainScreen.setLayout(new GridLayout(9,2)); mainScreen.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,0,2,0); addComponents(new Label("Button"), mainButton, mainScreen, gbc); addComponents(new Label("Choice"), new Choice(), mainScreen, gbc); addComponents(new Label("Label"), new Label("This is a Label"), mainScreen, gbc); addComponents(new Label("ScrollBar"), new Scrollbar(), mainScreen, gbc); addComponents(new Label("TextField"), new TextField(), mainScreen, gbc); Canvas canvas = new Canvas(); canvas.setBackground(Color.blue); addComponents(new Label("Canvas"), canvas, mainScreen, gbc); addComponents(new Label("List"), mainList, mainScreen, gbc); addComponents(new Label("TextArea"), new TextArea("hello world"), mainScreen, gbc); mainScreen.pack(); mainScreen.setVisible(true); mainButton.setActionCommand("Press Me"); mainButton.addActionListener(this); } private void addComponents(Component c1, Component c2, Container c, GridBagConstraints gbc) { gbc.gridwidth = GridBagConstraints.RELATIVE; c.add(c1, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; c.add(c2, gbc); } public static void main(String[] args){ GraphicTestRx graphictest = new GraphicTestRx(); graphictest.load(); } }
Reply With Quote