写了三部分程序,想要实现添加与查询商品情况的功能。运行情况如题。以下是三段代码,求高手帮忙。
//AddGoods.java
package goodadddelete;
import java.awt.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.UIManager;
class AddGoods extends Frame implements ActionListener{
Frame ff = new Frame("商品管理");
Dialog1 agoods = new Dialog1(ff, "添加商品", false);
Dialog2 selectgoods = new Dialog2(ff, "查询商品", false);
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel10 = new JLabel();
private void go() {
ff.setBounds(200, 200, 400, 150);
ff.setLayout(null);
ff.add(jButton2);
ff.add(jButton1);
agoods.setBounds(20, 0, 400, 550);
jButton1.setBounds(new Rectangle(87, 81, 81, 30));
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton1.setText("添 加");
jButton2.setBounds(new Rectangle(234, 81, 81, 30));
jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton2.setText("查 询");
jLabel10.setFont(new java.awt.Font("黑体", Font.PLAIN, 22));
jLabel10.setText("商品管理");
jLabel10.setBounds(new Rectangle(153, 17, 132, 29));
ff.setVisible(true);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
agoods.setVisible(true);
}
public void actionPerformed(ActionEvent g)
{
selectgoods.setVisible(true);
} public static void main(String args[]) {
AddGoods e = new AddGoods();
e.go();
}
}
//Dialog1.java
package goodadddelete;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.BorderLayout;
import java.awt.Font;
public class Dialog1 extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JLabel jLabel8 = new JLabel();
Button button1 = new Button();
JComboBox jComboBox1 = new JComboBox();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
public Dialog1(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public Dialog1() {
this(new Frame(), "Dialog1", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
panel1.setSize(400,600);
this.getContentPane().setLayout(null);
panel1.setBounds(new Rectangle(0, 478, 1, 1));
jLabel1.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel1.setText("商品号");
jLabel1.setBounds(new Rectangle(62, 87, 56, 36));
jTextField1.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField1.setBounds(new Rectangle(153, 87, 133, 36));
jLabel2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel2.setText("名 称");
jLabel2.setBounds(new Rectangle(62, 137, 56, 36));
jLabel3.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel3.setText("种 类");
jLabel3.setBounds(new Rectangle(62, 189, 56, 36));
jLabel4.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel4.setText("单 价");
jLabel4.setBounds(new Rectangle(62, 232, 56, 36));
jLabel5.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel5.setText("是否打折");
jLabel5.setBounds(new Rectangle(62, 277, 75, 36));
jLabel6.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel6.setText("折 扣");
jLabel6.setBounds(new Rectangle(62, 329, 56, 36));
jLabel7.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel7.setText("数 量");
jLabel7.setBounds(new Rectangle(62, 392, 56, 36));
jTextField2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField2.setBounds(new Rectangle(153, 137, 133, 36));
jTextField3.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField3.setBounds(new Rectangle(153, 232, 80, 36));
jTextField4.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField4.setBounds(new Rectangle(153, 392, 80, 36));
this.getContentPane().setBackground(SystemColor.controlLtHighlight);
this.setForeground(Color.cyan);
jLabel8.setFont(new java.awt.Font("黑体", Font.PLAIN, 25));
jLabel8.setText("添加商品");
jLabel8.setBounds(new Rectangle(130, 26, 117, 33));
button1.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 20));
button1.setLabel("完 成");
button1.setBounds(new Rectangle(147, 468, 105, 39));
jComboBox1.setBounds(new Rectangle(155, 329, 92, 36));
jRadioButton1.setBackground(Color.white);
jRadioButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jRadioButton1.setText("是");
jRadioButton1.setBounds(new Rectangle(153, 277, 70, 36));
jRadioButton2.setBackground(Color.white);
jRadioButton2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jRadioButton2.setText("否");
jRadioButton2.setBounds(new Rectangle(237, 277, 70, 36));
this.getContentPane().add(panel1, null);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jLabel6);
this.getContentPane().add(jLabel7);
this.getContentPane().add(jTextField4);
this.getContentPane().add(jTextField3);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel8);
this.getContentPane().add(button1);
this.getContentPane().add(jComboBox1);
this.getContentPane().add(jRadioButton1);
this.getContentPane().add(jRadioButton2);
}
}
//Dialog2.java
package goodadddelete;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.BorderLayout;
public class Dialog2 extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
Button button1 = new Button();
JLabel jLabel3 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel4 = new JLabel();
public Dialog2(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public Dialog2() {
this(new Frame(), "Dialog2", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
panel1.setBounds(0,500,381,484);
panel1.setBackground(SystemColor.activeCaptionText);
panel1.setBounds(new Rectangle(0, 15, 1, 1));
this.getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("黑体", Font.PLAIN, 25));
jLabel1.setText("查询商品");
jLabel1.setBounds(new Rectangle(134, 12, 113, 39));
jTextField2.setBounds(new Rectangle(152, 124, 140, 28));
jLabel4.setFont(new java.awt.Font("Arial Black", Font.PLAIN, 20));
jLabel4.setText("OR");
jLabel4.setBounds(new Rectangle(15, 100, 36, 41));
button1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
this.getContentPane().add(panel1, null);
jLabel3.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));
jLabel3.setText("商品号");
jLabel3.setBounds(new Rectangle(67, 86, 59, 28));
button1.setLabel("查 询");
button1.setBounds(new Rectangle(130, 175, 84, 35));
jTextField1.setBounds(new Rectangle(152, 86, 140, 28));
jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));
jLabel2.setText("名 称");
jLabel2.setBounds(new Rectangle(67, 124, 59, 28));
this.setForeground(SystemColor.activeCaptionText);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel4);
this.getContentPane().add(button1); }
}
//AddGoods.java
package goodadddelete;
import java.awt.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.UIManager;
class AddGoods extends Frame implements ActionListener{
Frame ff = new Frame("商品管理");
Dialog1 agoods = new Dialog1(ff, "添加商品", false);
Dialog2 selectgoods = new Dialog2(ff, "查询商品", false);
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel10 = new JLabel();
private void go() {
ff.setBounds(200, 200, 400, 150);
ff.setLayout(null);
ff.add(jButton2);
ff.add(jButton1);
agoods.setBounds(20, 0, 400, 550);
jButton1.setBounds(new Rectangle(87, 81, 81, 30));
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton1.setText("添 加");
jButton2.setBounds(new Rectangle(234, 81, 81, 30));
jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton2.setText("查 询");
jLabel10.setFont(new java.awt.Font("黑体", Font.PLAIN, 22));
jLabel10.setText("商品管理");
jLabel10.setBounds(new Rectangle(153, 17, 132, 29));
ff.setVisible(true);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
agoods.setVisible(true);
}
public void actionPerformed(ActionEvent g)
{
selectgoods.setVisible(true);
} public static void main(String args[]) {
AddGoods e = new AddGoods();
e.go();
}
}
//Dialog1.java
package goodadddelete;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.BorderLayout;
import java.awt.Font;
public class Dialog1 extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
JLabel jLabel8 = new JLabel();
Button button1 = new Button();
JComboBox jComboBox1 = new JComboBox();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
public Dialog1(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public Dialog1() {
this(new Frame(), "Dialog1", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
panel1.setSize(400,600);
this.getContentPane().setLayout(null);
panel1.setBounds(new Rectangle(0, 478, 1, 1));
jLabel1.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel1.setText("商品号");
jLabel1.setBounds(new Rectangle(62, 87, 56, 36));
jTextField1.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField1.setBounds(new Rectangle(153, 87, 133, 36));
jLabel2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel2.setText("名 称");
jLabel2.setBounds(new Rectangle(62, 137, 56, 36));
jLabel3.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel3.setText("种 类");
jLabel3.setBounds(new Rectangle(62, 189, 56, 36));
jLabel4.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel4.setText("单 价");
jLabel4.setBounds(new Rectangle(62, 232, 56, 36));
jLabel5.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel5.setText("是否打折");
jLabel5.setBounds(new Rectangle(62, 277, 75, 36));
jLabel6.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel6.setText("折 扣");
jLabel6.setBounds(new Rectangle(62, 329, 56, 36));
jLabel7.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 18));
jLabel7.setText("数 量");
jLabel7.setBounds(new Rectangle(62, 392, 56, 36));
jTextField2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField2.setBounds(new Rectangle(153, 137, 133, 36));
jTextField3.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField3.setBounds(new Rectangle(153, 232, 80, 36));
jTextField4.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jTextField4.setBounds(new Rectangle(153, 392, 80, 36));
this.getContentPane().setBackground(SystemColor.controlLtHighlight);
this.setForeground(Color.cyan);
jLabel8.setFont(new java.awt.Font("黑体", Font.PLAIN, 25));
jLabel8.setText("添加商品");
jLabel8.setBounds(new Rectangle(130, 26, 117, 33));
button1.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 20));
button1.setLabel("完 成");
button1.setBounds(new Rectangle(147, 468, 105, 39));
jComboBox1.setBounds(new Rectangle(155, 329, 92, 36));
jRadioButton1.setBackground(Color.white);
jRadioButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jRadioButton1.setText("是");
jRadioButton1.setBounds(new Rectangle(153, 277, 70, 36));
jRadioButton2.setBackground(Color.white);
jRadioButton2.setFont(new java.awt.Font("楷体_GB2312", Font.PLAIN, 15));
jRadioButton2.setText("否");
jRadioButton2.setBounds(new Rectangle(237, 277, 70, 36));
this.getContentPane().add(panel1, null);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jLabel6);
this.getContentPane().add(jLabel7);
this.getContentPane().add(jTextField4);
this.getContentPane().add(jTextField3);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel8);
this.getContentPane().add(button1);
this.getContentPane().add(jComboBox1);
this.getContentPane().add(jRadioButton1);
this.getContentPane().add(jRadioButton2);
}
}
//Dialog2.java
package goodadddelete;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.BorderLayout;
public class Dialog2 extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
Button button1 = new Button();
JLabel jLabel3 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel4 = new JLabel();
public Dialog2(Frame owner, String title, boolean modal) {
super(owner, title, modal);
try {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public Dialog2() {
this(new Frame(), "Dialog2", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
panel1.setBounds(0,500,381,484);
panel1.setBackground(SystemColor.activeCaptionText);
panel1.setBounds(new Rectangle(0, 15, 1, 1));
this.getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("黑体", Font.PLAIN, 25));
jLabel1.setText("查询商品");
jLabel1.setBounds(new Rectangle(134, 12, 113, 39));
jTextField2.setBounds(new Rectangle(152, 124, 140, 28));
jLabel4.setFont(new java.awt.Font("Arial Black", Font.PLAIN, 20));
jLabel4.setText("OR");
jLabel4.setBounds(new Rectangle(15, 100, 36, 41));
button1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
this.getContentPane().add(panel1, null);
jLabel3.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));
jLabel3.setText("商品号");
jLabel3.setBounds(new Rectangle(67, 86, 59, 28));
button1.setLabel("查 询");
button1.setBounds(new Rectangle(130, 175, 84, 35));
jTextField1.setBounds(new Rectangle(152, 86, 140, 28));
jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 18));
jLabel2.setText("名 称");
jLabel2.setBounds(new Rectangle(67, 124, 59, 28));
this.setForeground(SystemColor.activeCaptionText);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jTextField1);
this.getContentPane().add(jTextField2);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel4);
this.getContentPane().add(button1); }
}
{
agoods.setVisible(true);
}
public void actionPerformed(ActionEvent g)
{
selectgoods.setVisible(true);
}
同名 同参数 , 你编辑的时候不报错误么?
Frame ff = new Frame("商品管理");
Dialog1 agoods = new Dialog1(ff, "添加商品", false);
Dialog2 selectgoods = new Dialog2(ff, "查询商品", false);
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel10 = new JLabel(); private void go() {
ff.setBounds(200, 200, 400, 150);
ff.setLayout(null);
ff.add(jButton2);
ff.add(jButton1);
agoods.setBounds(20, 0, 400, 550);
jButton1.setBounds(new Rectangle(87, 81, 81, 30));
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton1.setText("添 加");
jButton2.setBounds(new Rectangle(234, 81, 81, 30));
jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton2.setText("查 询");
jLabel10.setFont(new java.awt.Font("黑体", Font.PLAIN, 22));
jLabel10.setText("商品管理");
jLabel10.setBounds(new Rectangle(153, 17, 132, 29));
ff.setVisible(true);
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
search();
}
});
jButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add();
}
});
} public void actionPerformed(ActionEvent e) {
agoods.setVisible(true);
selectgoods.setVisible(true);
}
public void search(){
agoods.setVisible(true);
}
public void add(){
selectgoods.setVisible(true);
}
public static void main(String args[]) {
AddGoods e = new AddGoods();
e.go();
}}
public class AddGoods extends Frame implements ActionListener {
Frame ff = new Frame("商品管理");
Dialog1 agoods = new Dialog1(ff, "添加商品", false);
Dialog2 selectgoods = new Dialog2(ff, "查询商品", false);
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel10 = new JLabel(); private void go() {
ff.setBounds(200, 200, 400, 150);
ff.setLayout(null);
ff.add(jButton2);
ff.add(jButton1);
agoods.setBounds(20, 0, 400, 550);
jButton1.setBounds(new Rectangle(87, 81, 81, 30));
jButton1.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton1.setText("添 加");
jButton2.setBounds(new Rectangle(234, 81, 81, 30));
jButton2.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
jButton2.setText("查 询");
jLabel10.setFont(new java.awt.Font("黑体", Font.PLAIN, 22));
jLabel10.setText("商品管理");
jLabel10.setBounds(new Rectangle(153, 17, 132, 29));
ff.setVisible(true);
//添加按钮监听
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add();
}
});
//查询按钮监听
jButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
search();
}
});
} public void actionPerformed(ActionEvent e) {
agoods.setVisible(true);
selectgoods.setVisible(true);
} //设置添加产品面板显示
public void add(){
agoods.setVisible(true);
}
//设置查询产品面板显示
public void search(){
selectgoods.setVisible(true);
}
public static void main(String args[]) {
AddGoods e = new AddGoods();
e.go();
}}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jButton1)
{agoods.setVisible(true); }
if(e.getSource() == jButton2)
{selectgoods.setVisible(true);}
}
if(e.getSource() == jButton1)
{agoods.setVisible(true); }
if(e.getSource() == jButton2)
{selectgoods.setVisible(true);}
}
方法很好用