Всем привет, почему-то не отображается кнопка во фрейме, хотя ексепшн работает нормально и всплывающее меню тоже. Может кто подскажет, вот код:
| Код | import javax.swing.*; import java.awt.EventQueue; import java.awt.event.ActionEvent; //import java.awt.event.ActionListener; import java.awt.*;
public class ExceptionsTest3 {
public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ public void run() { MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }
}
class MyFrame extends JFrame { public MyFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setTitle("Testing exceptions..."); setLocationByPlatform(true); //JButton yellowButton = new JButton("Yellow"); // JButton blueButton = new JButton("Blue"); // JButton redButton = new JButton("Red"); //JButton button = new JButton("Show text"); buttonPanel = new JPanel(); Action yellowAction = new ColorAction("Yellow", Color.YELLOW); Action blueAction = new ColorAction("Blue", Color.BLUE); Action redAction = new ColorAction("Red", Color.RED); Action buttonAction = new ButtonAction("Show text"); buttonPanel.add(new JButton(yellowAction)); buttonPanel.add(new JButton(blueAction)); buttonPanel.add(new JButton(redAction)); buttonPanel.add(new JButton(buttonAction)); add(buttonPanel); InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow"); imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue"); imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red"); ActionMap amap = buttonPanel.getActionMap(); amap.put("panel.yellow", yellowAction); amap.put("panel.blue", blueAction); amap.put("panel.red", redAction); //yellowButton.addActionListener(yellowAction); //blueButton.addActionListener(blueAction); //redButton.addActionListener(redAction); } public class ButtonAction extends AbstractAction { public ButtonAction(String s) { putValue(Action.SHORT_DESCRIPTION, "Display text"); } public void actionPerformed(ActionEvent event) { Graphics g = buttonPanel.getGraphics(); Font f = new Font("Times New Roman", Font.BOLD, 18); g.setFont(f); g.drawString("The button has been clicked.",100,100); }
}
public class ColorAction extends AbstractAction {
public ColorAction(String name, Color c) { putValue(Action.NAME, name); putValue(Action.SHORT_DESCRIPTION, "Set panel color to "+name.toLowerCase()); putValue("color", c); } public void actionPerformed(ActionEvent event) { Color с = (Color) getValue("color"); buttonPanel.setBackground(с); buttonPanel.repaint(); } }
private JPanel buttonPanel; public static int DEFAULT_WIDTH = 450; public static int DEFAULT_HEIGHT = 300; }
|
Вот скриншот: http://ipicture.ru/Gallery/Viewfull/1179862.html
Заранее спасибо. |