我无法设置我的新窗口布局。我有一个菜单和子菜单。我的子菜单上有一个动作监听器,它将我引导到新窗口。问题是我无法将其设置为给定的布局。这是我的代码:
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(){...