在窗口中间添加多个JPanel

时间:2015-07-13 20:26:31

标签: java swing

我想创建一个输入JFrame,其中程序读取三个字段(模型,周和计划),插入一行后,用户可以选择在不同的行上输入新值,这是按下完成的带有图片添加图标的JLabel。 我的期望是能够在最后一个(JPanel全局变量)的正下方添加一个新的JPanel lastContent(在子类中称为body),并且能够以用户身份删除或添加新的package marquesina; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import org.jdesktop.swingx.HorizontalLayout; import org.jdesktop.swingx.VerticalLayout; public class JModificaciones extends Container { private JPanel lastContent; public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("DEMO"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. JModificaciones mods = new JModificaciones(); frame.setContentPane(mods); //Display the window. frame.pack(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation( dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2); frame.setVisible(true); }); } public JModificaciones() { initComponents(); System.out.println("New Panel Created"); } private void initComponents() { JPanel jHeader = new JPanel(); JLabel jLMod = new JLabel(); JLabel jLSem = new JLabel(); JLabel jLPlan = new JLabel(); JPanel jFooter = new JPanel(); JButton jGuardar = new JButton(); JButton jCancelar = new JButton(); setLayout(new VerticalLayout(10)); //HEADER jHeader.setLayout(new HorizontalLayout()); jLMod.setText("Model"); jHeader.add(jLMod); jLWeek.setText("Week"); jHeader.add(jLWeek); jLPlan.setText("Plan"); jHeader.add(jLPlan); add(jHeader); //CONTENT add(new jContent()); //FOOTER jGuardar.setText("Save"); jFooter.add(jGuardar); jCancelar.setText("Cancel"); jFooter.add(jCancel); add(jFooter); } public class jContent extends JPanel { JLabel jAdd = new javax.swing.JLabel(); JLabel jDelete = new javax.swing.JLabel(); public jContent() { JPanel body = new JPanel(new HorizontalLayout()); JTextField jModel = new JTextField(); JTextField jWeek = new JTextField(); JTextField jPlan = new JTextField(); body.setLayout(new org.jdesktop.swingx.HorizontalLayout()); jModel.setPreferredSize(new java.awt.Dimension(100, 28)); body.add(jModel); jWeek.setPreferredSize(new java.awt.Dimension(100, 28)); body.add(jWeek); jPlan.setPreferredSize(new java.awt.Dimension(100, 28)); body.add(jPlan); jAdd.setIcon( new javax.swing.ImageIcon( getClass().getResource("add.png"))); jAdd.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(java.awt.event.MouseEvent evt) { } }); body.add(jAdd); jDelete.setIcon( new javax.swing.ImageIcon( getClass().getResource("delete.png"))); jDelete.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(java.awt.event.MouseEvent evt) { remove(lastContent); } }); body.add(jDelete); add(body); } } 需要。 以下是我目前的代码:

JPanel

}

但是我无法添加新的jAdd JLabel(我想在用户点击JPanel时创建),我无法想出一种方法来引用{ {1}}我想在上面的代码中添加我的新组件并使用add()或remove(),只需引用MouseListener,而不是在sublcass中创建的JPanel。 ..

0 个答案:

没有答案