gpt4 book ai didi

java - 设置菜单对象的布局

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:51 30 4
gpt4 key购买 nike

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

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(){...

最佳答案

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

关于java - 设置菜单对象的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10418528/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com