2012-05-02 6 views
1

新しいウィンドウレイアウトを設定できません。私はメニューとサブメニューを持っています。私は新しいウィンドウに私を導くサブメニューにアクションリスナーを持っています。問題は、私は与えられたレイアウトに設定できないということです。ここに私のコードは次のとおりです。メニューオブジェクトのレイアウトを設定する

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Converter extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    private MoneyDetails convertMe = new MoneyDetails(); 
    private JLabel tlLabel = new JLabel("  Amount of TL"); 
    private JLabel dollarsLabel = new JLabel("Amount of Dollars"); 
    private JTextField tlField = new JTextField("0.0"); 
    private JTextField dollarsField = new JTextField("0.0"); 
    private JButton tlButton = new JButton("Convert to $"); 
    private JButton dollarsButton = new JButton("<<< Convert to TL"); 
    private JButton setRates = new JButton("Set Rates"); 

    private JMenuBar menuBar = new JMenuBar(); // Window menu bar 
    public Converter(String title) { 
     setTitle(title); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setJMenuBar(menuBar); // Add the menu bar to the window 
     JMenu fileMenu = new JMenu("File"); // Create File menu 
     JMenu elementMenu = new JMenu("Elements"); // Create Elements menu 
     JMenuItem subTest = new JMenuItem("Test"); 

     // Here is the problem 
     subTest.addActionListener(new ActionListener(){  
      public void actionPerformed(ActionEvent actionEvent){ 
       Converter convert = new Converter(); 

       GridLayout expLay = new GridLayout(2,2,12,6); 
       convert.setLayout(expLay); 
       convert.getLayout(); 
       convert.doLayout(); 
       convert.setVisible(true); 
      } 
     }); 

     menuBar.add(fileMenu); // Add the file menu 
     menuBar.add(elementMenu); // Add the element menu 
     fileMenu.add(subTest); 
    } 

    public Converter() 
    { 
     JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6)); 
     dataPanel.add(tlLabel); 

     dataPanel.add(dollarsLabel); 
     dataPanel.add(tlField); 
     dataPanel.add(dollarsField); 
     JPanel buttonPanel = new JPanel(); 
     buttonPanel.add(tlButton); 
     buttonPanel.add(dollarsButton); 
     Container container = this.getContentPane(); 
     container.add(dataPanel, BorderLayout.CENTER); 
     container.add(buttonPanel, BorderLayout.SOUTH); 
     tlButton.addActionListener(new TLConverter()); 
     dollarsButton.addActionListener(new DollarsConverter()); 
     buttonPanel.add(setRates); 
    } 

    private class TLConverter implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      try 
      { 
       String input = tlField.getText(); 
       double tl = Double.parseDouble(input); 
       convertMe.setTL(tl); 
       double dollars = convertMe.getDollars(); 
       dollarsField.setText(String.format("%.2f", dollars)); 
      } 
      catch(Exception ex) 
      { 
       JOptionPane.showMessageDialog(null, "Please enter the amount that will be converted."); 
      } 
     } 
    } 

    private class DollarsConverter implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String input = dollarsField.getText(); 
      double dollars = Double.parseDouble(input); 
      convertMe.setDollars(dollars); 
      double tl = convertMe.getTL(); 
      tlField.setText(String.format("%.2f", tl)); 
     } 
    } 

    public static void main(String [] args) 
    { 
     System.out.println("bbb"); 
     Converter window = new Converter("Para Dönüstürücü"); 
     System.out.println("aaa"); 
     window.setBounds(30, 30, 300, 300); 
     window.setVisible(true); 
    /* Converter theGUI = new Converter(); 
     theGUI.setTitle("TL to $ or $ to TL Converter"); 
     theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     theGUI.pack(); 
     theGUI.setVisible(true); */ 
    } 
} 

問題はString引数でコンストラクタである:

+0

どのような_exactly_がうまくいかないかを詳しく教えてください。あなたは何をしようとしていますか、あなたは何を期待していますか? 'doLayout()'を直接呼び出してはいけませんが、Swingはそれをあなたに任せてください。 – Thomas

+0

もう一度問題がありますか?私のために(MoneyDetailsクラスを追加して)うまくいくと思う。私は "テスト"を押すと、小さなウィンドウのポップアップを得た - あなたの問題は小さいですか?もしデフォルトコンストラクタの最後に 'pack()'を呼び出すと、... –

+0

私は自分のメニューとサブメニューを持っています。サブメニューをクリックすると、新しいウィンドウが開きます。そのGridLayoutを設定したいと思います。私は多くの方法を試しましたが、どれも働いていませんでした。ここにスクリーンショットがあります。それはどうなっているのですか?http://desmond.imageshack.us/Himg189/scaled.php?server=189&filename=screenshot20120502at202.png&res=landing、それでは、http:// desmond.imageshack.us/Himg703/scaled.php?server=703&filename=screenshot20120502at202.png&res=landing –

答えて

1

subTest.addActionListener(new ActionListener(){...はデフォルトコンストラクタでpack()への呼び出しを追加します。

関連する問題