gpt4 book ai didi

java - 基本 GUI 出现问题

转载 作者:行者123 更新时间:2023-12-02 06:28:31 24 4
gpt4 key购买 nike

首先,我想说我已经搜索了这些论坛以及其他论坛来寻找我的问题的答案。我对 Java 和一般编程都很陌生。经过几个小时的努力,我想寻求一些帮助。

我正在尝试打开一个带有菜单栏的窗口(稍后会有一些图形)。这些选项是 3 个下拉菜单,选择后将执行任务。文件菜单允许我保存、加载和退出程序。现在,剩下的只需打印到屏幕上,让我知道大部分工作正在发挥作用。现在,我想对其进行设置,以便当我单击“创建卖家”选项时,它将打开一个包含 3 个字段的新对话框。我想在其中一个中输入一个字符串,在另外两个中输入两个 double 。然后我会将它们存储为变量并将它们发送到另一个函数。我已经在这部分上停留太久了。请记住,虽然这个问题之前可能已经得到解答,但我认为我对 Java 的能力还不够,还无法理解答案。如果这要求还不算过分的话,请尝试为我简化一下。谢谢!

这是我正在上的类(class)。如果您需要,请随意要求更少或更多。

package seller;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import javax.swing.JTextField;





import datastore.Meteorite;
import datastore.continents;
import datastore.customers;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;


public class BaseFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;

JFileChooser myChooser = new JFileChooser(".");

JMenuItem menuItemSave;
JMenuItem menuItemLoad;
JMenuItem menuItemExit;
JMenuItem menuItemCreateSeller;
JMenuItem menuItemUpdateSeller;
JMenuItem menuItemRemoveSeller;
JMenuItem menuItemCreateMeteorite;
JMenuItem menuItemUpdateMeteorite;
JMenuItem menuItemRemoveMeteorite;
JMenuItem menuItemCreateOneLine;
continents worldMap = new continents();
boolean[][] mapArray = null;

int selection = 1;
String shopName = null;
Map myMap= new Map();
Scanner myScanner = new Scanner(System.in);
Menu myMenu = new Menu();
seller seller= new seller();
Meteorites myMeteorite = new Meteorites();
customers sellers= new customers(0,0,"test");
customers buyers= new customers(0, 0, "test");
ArrayList <Meteorite> myMeteorites = new ArrayList<Meteorite>();
ArrayList <customers> mySellers= new ArrayList<customers>();
ArrayList <customers> myBuyers= new ArrayList<customers>();
customers people= new customers(0,0,"test");


public void makeGui()
{
this.setTitle("Phase 3");

JMenuBar mb = new JMenuBar();
this.setJMenuBar(mb);

//Load and Save used from class example
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);

menuItemSave = new JMenuItem("Save");
menuItemSave.addActionListener(this);

menuItemLoad = new JMenuItem("Load");
menuItemLoad.addActionListener(this);

menuItemExit = new JMenuItem("Exit");
menuItemExit.addActionListener(this);

fileMenu.add(menuItemSave);
fileMenu.add(menuItemLoad);
fileMenu.add(menuItemExit);


//seller menu

JMenu sellerMenu = new JMenu("Seller");
mb.add(sellerMenu);

menuItemCreateSeller = new JMenuItem("Create Seller");
menuItemCreateSeller.addActionListener(this);

menuItemUpdateSeller = new JMenuItem("Update Seller");
menuItemUpdateSeller.addActionListener(this);

menuItemRemoveSeller = new JMenuItem("Remove Seller");
menuItemRemoveSeller.addActionListener(this);

sellerMenu.add(menuItemCreateSeller);
sellerMenu.add(menuItemUpdateSeller);
sellerMenu.add(menuItemRemoveSeller);

//handle meteorite menu

JMenu handleMeteoriteMenu = new JMenu("Handle Meteorite");
mb.add(handleMeteoriteMenu);

menuItemCreateMeteorite = new JMenuItem("Create Meteorite");
menuItemCreateMeteorite.addActionListener(this);

menuItemUpdateMeteorite = new JMenuItem("Update Meteorite");
menuItemUpdateMeteorite.addActionListener(this);

menuItemRemoveMeteorite = new JMenuItem("Remove Meteorite");
menuItemRemoveMeteorite.addActionListener(this);

menuItemCreateOneLine = new JMenuItem("Create Meteorite(1 line)");
menuItemCreateOneLine.addActionListener(this);

handleMeteoriteMenu.add(menuItemCreateMeteorite);
handleMeteoriteMenu.add(menuItemUpdateMeteorite);
handleMeteoriteMenu.add(menuItemRemoveMeteorite);
handleMeteoriteMenu.add(menuItemCreateOneLine);


JPanel tPanel = new JPanel();
this.add(tPanel);
this.pack();
this.setSize(new Dimension(640,480));
this.setVisible(true);


}
String myName;
double myLong;
double myLat;
@Override
public void actionPerformed(ActionEvent e)
{

Object source=null;
source=e.getSource();

if (source == menuItemLoad)
{

mapArray = worldMap.createMap();

myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = myChooser.showOpenDialog(this);

//if user clicked Cancel button, return
if (result == JFileChooser.CANCEL_OPTION)
System.exit(1);

File myFile = myChooser.getSelectedFile(); //get file

//display error if invalid
if ( ( myFile == null) || (myFile.getName().equals("")))
{
JOptionPane.showMessageDialog(this, "Invalid Name", "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

myMeteorites= myMeteorite.loadMeteorite(myMeteorites, myFile);
sellers.loadSeller(mySellers);
buyers.loadBuyer(myBuyers);

}

if (source == menuItemSave)
{
mapArray = worldMap.createMap();

myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = myChooser.showOpenDialog(this);

//if user clicked Cancel button, return
if (result == JFileChooser.CANCEL_OPTION)
System.exit(1);

File myFile = myChooser.getSelectedFile(); //get file

//display error if invalid
if ( ( myFile == null) || (myFile.getName().equals("")))
{
JOptionPane.showMessageDialog(this, "Invalid Name", "Invalid Name", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

myMeteorites= myMeteorite.saveMeteorite(myMeteorites, myFile);
sellers.saveSeller(mySellers);
buyers.saveBuyer(myBuyers);

}

if (source == menuItemExit)
{
System.exit(1); //exit system
}

if (source == menuItemCreateSeller)
{

JFrame myFrame = new JFrame("Create a Seller");
JPanel myPanel = new JPanel();
JLabel myLabel1 = new JLabel();
JLabel myLabel2 = new JLabel();
JLabel myLabel3 = new JLabel();
final JTextField myText1 = new JTextField(30);
final JTextField myText2 = new JTextField(30);
final JTextField myText3 = new JTextField(30);
setDefaultCloseOperation(EXIT_ON_CLOSE);

myLabel1.setText("Enter Name");
myLabel2.setText("Enter Longitude");
myLabel3.setText("Enter Latitude");

myPanel.add(myLabel1);
myPanel.add(myText1);
myPanel.add(myLabel2);
myPanel.add(myText2);
myPanel.add(myLabel3);
myPanel.add(myText3);

myFrame.add(myPanel);
myFrame.setSize(350,200);
myFrame.setVisible(true);

myText1.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
myName = myText1.getText();
}

});

myName = myText1.getText();
//myLong = Double.parseDouble(myText2.getText());
//myLat = Double.parseDouble(myText3.getText());


System.out.println(myName);
//mySellers=people.createCust(mySellers, myLong, myLat, myName);
//mySellers=people.saveSeller(mySellers);
}

if (source == menuItemRemoveSeller)
{
System.out.println("remove seller");
}

if (source == menuItemUpdateSeller)
{
System.out.println("update seller");
}

if (source == menuItemCreateMeteorite)
{
System.out.println("create meteorite");
}

if (source == menuItemUpdateMeteorite)
{
System.out.println("updateMeteorite");
}
if (source == menuItemRemoveMeteorite)
{
System.out.println("remove meteorite");
}

if (source == menuItemCreateOneLine)
{
System.out.println("create one line");
}

}
}

最佳答案

请参阅此示例以了解我正在谈论的内容。虽然我建议不要使用多个框架,但这将适合您想要做的事情。正如 @MadProgrammer 建议的那样,研究创建 JDialogs

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

public class BaseFrame extends JFrame {

private String name;
private double value1;
private double value2;

private final JMenuBar jMenuBar = new JMenuBar();
private final JMenu jmOpen = new JMenu("Open");
JMenuItem menuItemCreateSeller = new JMenuItem("Create Seller");
InnerFrame innerFrame = new InnerFrame();

public BaseFrame() {

innerFrame.setResizable(false);
jmOpen.add(menuItemCreateSeller);
jMenuBar.add(jmOpen);

setJMenuBar(jMenuBar);

menuItemCreateSeller.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
innerFrame.setVisible(true);
}
});

setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
Frame frame = new BaseFrame();
}
});
}

private class InnerFrame extends JFrame {

JPanel myPanel = new JPanel();
JLabel myLabel1 = new JLabel("Enter Name");
JLabel myLabel2 = new JLabel("Enter Longitude");
JLabel myLabel3 = new JLabel("Enter Latitude");
JLabel jlblStatus = new JLabel(" ");
JTextField myText1 = new JTextField(10);
JTextField myText2 = new JTextField(10);
JTextField myText3 = new JTextField(10);

JButton jbtPrint = new JButton("Print");

public InnerFrame() {

myPanel.add(myLabel1);
myPanel.add(myText1);
myPanel.add(myLabel2);
myPanel.add(myText2);
myPanel.add(myLabel3);
myPanel.add(myText3);

JPanel p1 = new JPanel(new GridLayout(1,2));
p1.add(jlblStatus);
p1.add(jbtPrint);
jlblStatus.setHorizontalAlignment(SwingConstants.LEFT);
jlblStatus.setForeground(Color.RED);

jbtPrint.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
name = myText1.getText();
value1 = Double.parseDouble(myText2.getText());
value2 = Double.parseDouble(myText3.getText());
System.out.println(name);
double total = value1 + value2;
System.out.println(total);

setVisible(false);
} catch (NumberFormatException ex) {
jlblStatus.setText("Invalid Input");
}
}
});

add(myPanel, BorderLayout.CENTER);
add(p1, BorderLayout.SOUTH);
setSize(180, 220);
setLocationRelativeTo(null);
setVisible(false);

}
}
}

关于java - 基本 GUI 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20295399/

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