【发布时间】:2015-06-18 05:01:47
【问题描述】:
我知道我仍然需要将 string 转换为 int 并为 providerID 和 providerPrice 加倍,以便我可以设置它们,然后再存储它们;我希望我能很好地解决这个问题。我似乎找不到的是如何使 JLabels 正确对齐。 cloudBack 标签应该是主背景,每组 JLabel 和 JTextField 应该在与主背景重叠的单独一行上,底部有 3 个 JButton。我如何让他们去正确的地方?顺便说一句,源 .png 是 400x224 像素 包新提供者;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/*@Michael Christopher, author/owner */
public class NewProvider {
/** @param args the command line arguments*/
public static void createGUI() {
// Create the window
JFrame mainFrame = new JFrame ("NewProvider");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create the MenuBar
JMenuBar maintenanceMenuBar = new JMenuBar ();
maintenanceMenuBar.setOpaque(true);
maintenanceMenuBar.setBackground(new Color(176,224,230));
maintenanceMenuBar.setPreferredSize(new Dimension(400, 20));
//create variables
int providerID;
String providerName;
Double providerPrice;
int addProviderCheck;
String[] options = {"Yes","No"};
//create image icon, labels, and JTextFields
JLabel cloudBack;
cloudBack = new JLabel (new ImageIcon("images/CloudBack.png"));
JLabel labelID = new JLabel ("New ProviderID");
JTextField textID = new JTextField("providerID \n", 10);
JLabel labelName = new JLabel ("New Provider Name");
JTextField textName = new JTextField("Provider Name \n", 20);
JLabel labelPrice = new JLabel ("New Provider Price");
JTextField textPrice = new JTextField ("Price \n", 5);
//make Submit, Clear, & Exit buttons
JButton submit = new JButton("Submit");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");
/*Set the FlowLayout to arrange the window, setting labels, text fields,
menubar, background, and orientation*/
FlowLayout newProviderLayout = new FlowLayout();
mainFrame.setLayout(newProviderLayout);
mainFrame.setJMenuBar(maintenanceMenuBar);
mainFrame.add(cloudBack, BorderLayout.CENTER);
mainFrame.add(labelID);
mainFrame.add(textID);
mainFrame.add(labelName);
mainFrame.add(textName);
mainFrame.add(labelPrice);
mainFrame.add(textPrice);
mainFrame.add(submit);
mainFrame.add(clear);
mainFrame.add(exit);
//ActionListener for Submit button
submit.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e) {
String ID = textID.getText();
String Name = textName.getText();
String Price = textPrice.getText();
int submitPress = JOptionPane.showOptionDialog(null,
"ProviderID: " + ID + "\n" + "Provider Name: " + Name +"\n" + "Provider Price: " + Price,
"Please Verify Content",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null, options,options[0]);
if (submitPress <= 0) {
//Store Displayed data
System.out.println(ID);
System.out.println(Name);
System.out.println(Price);
/*providerID = ID;
providerName = Name;
providerPrice = Price; */
textID.setText("Confirmed");
}
else if (submitPress > 0) {
textID.setText("providerID");
textName.setText("Provider Name");
textPrice.setText("Price"); }
}
});
//ActionListener for clear button
clear.addActionListener(new ActionListener()
{ @Override
public void actionPerformed(ActionEvent e) {
textID.setText("providerID");
textName.setText("Provider Name");
textPrice.setText("Price"); }
});
//ActionListener for Exit Button
exit.addActionListener(new ActionListener()
{ @Override
public void actionPerformed(ActionEvent e) {
mainFrame.setVisible(false);
mainFrame.dispose(); }
});
//verify intent to add new provider to system before continuing
addProviderCheck = JOptionPane.showOptionDialog(null,
"This will add a new service provider to the database.\n"
+ "Are you sure you wish to continue?",
"Please Verify Intent",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null, options,options[0]);
if (addProviderCheck <= 0) {
//Display Window
mainFrame.pack();
mainFrame.setSize(400, 400);
mainFrame.setVisible(true);}
else { //else close app
mainFrame.setVisible(false);
mainFrame.dispose();
}
}
public static void main(String[] args){
//draw and show the GUI
javax.swing.SwingUtilities.invokeLater(() -> {
createGUI();
});
//store new provider data
}
}
【问题讨论】:
-
请注意,您发布了不可编译的代码。请解决这个问题。
-
@Hovercraft Full Of Eels - 包 newprovider;应该在代码块中作为第一行;除此之外,它是我工作代码的副本>粘贴 - 这是你的意思还是我错过了一个更大的问题? MadProgrammer - 看看你的例子,ty。 Hovercraft 的第二个答案 - 谢谢。我今天将花时间研究 JPanel 和 paintComponent,以及您引用的其他 2 个布局。
-
显然我错了能够弄清楚 int 和 double。我可以将它们解析为新变量,但无法将它们设置为输入值。 Netbeans 一直告诉我“从内部类引用的局部变量必须是最终的或有效的最终变量。”如果我将 3 个变量设置为定义它们的最终变量,netbeans 然后告诉我删除最终变量,然后重新启动循环。
-
在提交按钮的 if 块中: int pID = Integer.parseInt(ID);双 dPrice = Double.parseDouble(Price);提供者 ID = pID;提供者名称 = 名称; providerPrice = dPrice;
标签: java swing jframe jbutton flowlayout