qq_43006674 2020-08-21 18:46 采纳率: 0%
浏览 112

java 在卡片布局里如何快速的添加同一个jpanel

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import com.bn.font.FontClass;
import com.bn.util.MyScreen;

public class ShopInformation extends JFrame{
    private static final long  serialVersionUID = 1L;//反序列化标记
    ImageIcon img;
    public static InformationNum addshop;
    public static JPanel jp = new JPanel();
    public ShopInformation () {
        this.setTitle("添加信息");
        addshop = new InformationNum ();
        img = new ImageIcon ("res/zol.png");
        this.setIconImage(img.getImage());
        this.add(addshop);
        int widthscreen = MyScreen.getMyScreenWidth();
        int hightscreen = MyScreen.getMyScreenHeight();
        int widthframe = 500;
        int hightframe = 400;
        this.setBounds(widthscreen/2-widthframe/2,hightscreen/2-hightframe/2,widthframe,hightframe);
        this.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e)
                    {
                        dispose();
                    }
                }
                );
        FontClass.loadIndyFont();
        this.setResizable(true);
        this.setVisible(true);
    }
}

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.bn.font.FontClass;

public class AddInformation extends JPanel {
    JTextArea inform=new JTextArea();
    JLabel ininform=new JLabel("请添加商品信息:(注意:一次仅能输入一条信息,并用冒号注明)");
    JScrollPane js=new JScrollPane(inform);
    JButton sure=new JButton("确定");
    JButton next=new JButton("下一个");
    JButton back=new JButton("上一个");
    JButton cancel=new JButton("取消");
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    int n;
    CardLayout cl=new CardLayout();
    public AddInformation(int n)
    {FontClass.loadIndyFont();
    this.setLayout(null);
    this.n=n;
    JPanel panel[]=new JPanel[n]; 
//    System.out.print(n);
    p1.setBounds(0,0,500,290);
    p1.setLayout(cl);
    p2.setLayout(null);
     p2.add(js);
        p2.add(ininform);
        ininform.setBounds(10,0,450,30);
        js.setBounds(10,30,400,100);
        inform.setLineWrap(true);
    for(int i=0;i<n;i++)
    {
        panel[i]=p2;
    }
    for(int i=0;i<n;i++)
    {
        p1.add(panel[i]);
    }
    this.add(p1);
    this.add(sure);
    this.add(next);
    this.add(back);
    this.add(cancel);
    sure.setBounds(30,300,80,25);
    next.setBounds(115,300,80,25);
    back.setBounds(200, 300, 80, 25);
    cancel.setBounds(285,300,80,25);
    next.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
//          if(inform.getText().equals(""))
//          {
//              JOptionPane.showMessageDialog(null, "提示:商品信息为空!","提示",JOptionPane.INFORMATION_MESSAGE);
//          }else{
            cl.next(p1);

        }
    });
    }
}
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class InformationNum extends JPanel{
    JPanel p2=new JPanel();
    JButton button=new JButton("添加");
    TextField t1=new TextField("",7);//作用是获得文本框的数量
    int n;
    public InformationNum()
    {
          this.setSize(300, 300);
          JLabel b1=new JLabel("信息个数");
          this.add(b1);
          this.add(t1);
          this.add(button);
          t1.addKeyListener(new KeyAdapter(){  
                public void keyTyped(KeyEvent e) { 
                    String key="0123456789.";  
                    if(key.indexOf(e.getKeyChar())<0){
                        e.consume();  
                    }            
                 }  
            });
          button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    int n;
                    String str;
                    if(t1.getText().equals("")){
                        JOptionPane.showMessageDialog(null, "提示:信息个数为空!","提示",JOptionPane.INFORMATION_MESSAGE);
                    }else{
                    str=t1.getText();
                    n=Integer.parseInt(str);
                    new InformFrame(n);
                    }
                }
            });
          this.setVisible(true);    
    }
}

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.bn.font.FontClass;
import com.bn.util.MyScreen;

public class InformFrame extends JFrame{
    private static final long  serialVersionUID = 1L;//反序列化标记
    ImageIcon img;
    public static AddInformation addshop;
    public static JPanel jp = new JPanel();
    int n;
    public InformFrame (int n) {
        this.setTitle("添加信息");
        this.n=n;
        addshop = new AddInformation (n);
        img = new ImageIcon ("res/zol.png");
        this.setIconImage(img.getImage());
        this.add(addshop);
        int widthscreen = MyScreen.getMyScreenWidth();
        int hightscreen = MyScreen.getMyScreenHeight();
        int widthframe = 500;
        int hightframe = 400;
        this.setBounds(widthscreen/2-widthframe/2,hightscreen/2-hightframe/2,widthframe,hightframe);
        this.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e)
                    {
                        dispose();
                    }
                }
                );
        FontClass.loadIndyFont();
        this.setResizable(true);
        this.setVisible(true);
}
}

我这个程序是想输入一个数字 然后在卡片布局里加入相应个数的jpanel 这个jpanel的样子都要和那个p2一样 我用jpanel数组也没有用 虽然是for循环 但还是只添加了一个JPanel 求助 急用

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿