【问题标题】:Setting layout of my menu object设置我的菜单对象的布局
【发布时间】:2012-05-02 17:13:49
【问题描述】:

我无法设置我的新窗口布局。我有一个菜单和子菜单。我的子菜单上有一个动作监听器,它将我引导到新窗口。问题是我无法将其设置为给定的布局。这是我的代码:

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 参数的构造函数:subTest.addActionListener(new ActionListener(){...

【问题讨论】:

  • 请详细说明究竟是什么不起作用。你想做什么,你期望什么,你会得到什么?你也不应该直接调用doLayout(),而是让 Swing 为你做这件事。
  • 又是什么问题?似乎对我很有用(在添加了一个假的MoneyDetails 类之后)。按“测试”时,我确实弹出了一个小窗口 - 你的问题是小吗?如果是这样,请尝试在默认构造函数的末尾调用pack()...
  • 我有我的菜单和子菜单。当我点击我的子菜单时,我有一个新窗口。我想设置它的 GridLayout。我尝试了很多方法,但都没有奏效。这是屏幕截图,这就是它现在的样子:desmond.imageshack.us/Himg189/…,这就是我希望它看起来像 desmond.imageshack.us/Himg703/…
  • 天哪,我怎么会忘记 pack().. 甚至没有检查它是否存在。非常感谢!
  • 没问题。我做了一个答案,以获得更多的代表。 :)

标签: java jbutton grid-layout jmenu


【解决方案1】:

在默认构造函数中添加对pack()的调用。

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多