GUI元素没有正确显示

时间:2016-05-01 11:53:29

标签: java swing

为什么界面不会出现,因为它已经写好了! 每当我尝试将一个元素添加到名为p1的Jpanlel时! 我应该更改什么才能使元素处于指定位置?  我没有改变任何事情!

 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 import java.util.Date;
 import java.util.Calendar;
 import javax.swing.JSpinner;
 import javax.swing.SpinnerDateModel;
 import java.text.*;


 public class MyJFrame extends  JFrame implements ActionListener {

    private ButtonGroup buttonGroup1;
    private ButtonGroup buttonGroup2;
    private JLabel dur;
    private JButton jButton1;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JPanel jPanel1;
    private JSpinner.DateEditor de2;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JPanel jPanel4;
    private JPanel jPanel5;
    private JRadioButton jRadioButton2;
    private JRadioButton jRadioButton6;
    private JScrollPane jScrollPane1;
    private JSpinner jSpinner1;
    private JSpinner jSpinner2;
    private JTabbedPane jTabbedPane1;
    private JTextArea jTextArea1;
    private JTextField jTextField1;
    private JRadioButton jYes;
    private JRadioButton rb1;
    private JRadioButton rb2;
    private JRadioButton rb3;
    private JTextField txt;
    private JSpinner spp2;
    private Date dat;
    private Date dat2;
    private JLabel jLabel8;
    private JTextArea jTextArea8;
    private JTextField jTextField8;
    private JButton jButton8;  
    private JButton jButton82; 
    private JButton jButton83;   
    private JLabel jLabel82;




  public MyJFrame(){
  JPanel p1=new JPanel();

  JPanel p2=new JPanel();
  JPanel p3=new JPanel();
  JPanel p4=new JPanel();
    //User r;
  jRadioButton6 = new JRadioButton();
  jPanel5 = new JPanel();
  buttonGroup1 = new ButtonGroup();
  buttonGroup2 = new ButtonGroup();

  jLabel1 = new JLabel("Event Title:");
  JTextField tit = new JTextField();       
  jLabel2 = new JLabel("Event Description:");
  JTextArea des = new JTextArea();        
  jScrollPane1 = new JScrollPane();
  jLabel3 = new JLabel("Start date:");
  jLabel4 = new JLabel("End date:");

  Date date = new Date();
  SpinnerDateModel sp = new SpinnerDateModel(date,null,null,Calendar.DAY_OF_MONTH);
  JSpinner spp = new JSpinner(sp);

  jSpinner1 = new JSpinner(sp);
  JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner1,"dd/mm/yyyy");
  jSpinner1.setEditor(de);



  Date date1 = new Date();
  SpinnerDateModel sp1 = new SpinnerDateModel       (date1,null,null,Calendar.HOUR_OF_DAY);
  JSpinner spp3 = new JSpinner(sp1);
  spp2 = new javax.swing.JSpinner(sp1);
  de2 = new JSpinner.DateEditor(spp2,"dd/mm/yyyy");
  spp2.setEditor(de2);

  jLabel5 = new JLabel("Is it a Recurrent event?");
  jYes = new JRadioButton("Yes");
  jRadioButton2 = new JRadioButton("No");
  buttonGroup1 = new ButtonGroup();
  buttonGroup2 = new ButtonGroup();        
  buttonGroup2.add(jYes);
  buttonGroup2.add(jRadioButton2);

  rb1 = new JRadioButton("Daily");
  rb2 = new JRadioButton("Weekly");
  rb3 = new JRadioButton("Monthly");
  buttonGroup1.add(rb1);
  buttonGroup1.add(rb2);
  buttonGroup1.add(rb3);
  dur = new JLabel("Duration:");
  txt = new JTextField();
  jButton1 = new JButton("Add Event");
  jTabbedPane1 = new JTabbedPane();
  jPanel1 = new JPanel();
  jPanel3 = new JPanel();
  jPanel4 = new JPanel();
  jPanel2 = new JPanel();
  jLabel6 = new JLabel();
  jLabel8 = new JLabel("Event ID:");
  jTextArea8 = new JTextArea(); 
  jTextField8= new JTextField();
  jButton8=new JButton("Find");
  jButton82=new JButton("Cancel"); 
  jButton83=new JButton("Save Agenda");
  jLabel82 = new JLabel();


  Container contentpane;
  contentpane=getContentPane();
  setSize(600,800);
  setLocation(600,140);
  JPanel tt=new JPanel();
  setDefaultCloseOperation(EXIT_ON_CLOSE);        
  tt.setLayout(new BorderLayout());
  getContentPane().add(tt);
  JTabbedPane tabbedPAne=new JTabbedPane();
  tabbedPAne.add("find event",p1);
  tabbedPAne.add("add event",p2);
  tabbedPAne.add("reminders",p3);
  tabbedPAne.add("save&load",p4);
  tt.add(tabbedPAne,BorderLayout.CENTER);
  p2.setLayout(null);
  jLabel1.setBounds(150,80,90,20);
  p2.add(jLabel1);
  tit.setBounds(240,80,170,20);
  p2.add(tit);
  jLabel2.setBounds(100,190,140,20);
  p2.add(jLabel2);
  des.setBounds(240,140,170,140);
  p2.add(des);
  jLabel3.setBounds(150,340,90,20);
  p2.add(jLabel3);
  jSpinner1.setBounds(240,340,160,20);
  p2.add(jSpinner1);
  jLabel4.setBounds(150,390,90,20);
  p2.add(jLabel4);
  spp2.setBounds(240,390,160,20);
  p2.add(spp2);        
  jLabel5.setBounds(120,450,190,20);
  p2.add(jLabel5);
  jYes.setBounds(300,450,50,20);
  jRadioButton2.setBounds(360,450,90,20);
  p2.add(jYes);
  p2.add(jRadioButton2);
  rb1.setBounds(140,500,60,20);
  rb2.setBounds(240,500,70,20);
  rb3.setBounds(340,500,70,20);
  p2.add(rb1);
  p2.add(rb2);
  p2.add(rb3);
  dur.setBounds(155,550,60,20);
  p2.add(dur);
  txt.setBounds(240,550,170,20);
  p2.add(txt);
  rb1.setEnabled(false);
  rb2.setEnabled(false);
  rb3.setEnabled(false);
  dur.setEnabled(false);
  txt.setEnabled(false);
  jButton1.setBounds(250,630,100,20);
  p2.add(jButton1);
        jLabel8.setBounds(90,30,60,20);
  p1.add(jLabel8);
  jTextField8.setBounds(80,30,45,20);
  p1.add(jTextArea8);
  jTextArea8.setBounds(100,80,130,70);
  p1.add(jTextArea8);
  jButton83.setBounds(30,510,50,20);
  p1.add(jButton83);
  jButton8.setBounds(100,45,50,20);
  p1.add(jButton8);
  jButton82.setBounds(90,45,50,20);
  p1.add(jButton82);
  jLabel82.setBounds(230,250,100,300);
  p1.add(jLabel82);

  setTitle("Tests' Planner");
  jYes.addActionListener(this); 
  jButton1.addActionListener(this); 

1 个答案:

答案 0 :(得分:0)

您缺少主要方法并将JFrame设置为可见。它现在正在运作。 您添加的几乎所有组件都使用.setBounds方法。这将它们设置在框架的特定位置。您可以尝试更改其参数以更改“组件”位置。无论如何,我建议使用Layouts,以便组件自动适应。您可以在此处详细了解布局:http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.util.Date;
import java.util.Calendar;

import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

import java.text.*;


public class MyJFrame extends  JFrame implements ActionListener {

private ButtonGroup buttonGroup1;
private ButtonGroup buttonGroup2;
private JLabel dur;
private JButton jButton1;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JPanel jPanel1;
private JSpinner.DateEditor de2;
private JPanel jPanel2;
private JPanel jPanel3;
private JPanel jPanel4;
private JPanel jPanel5;
private JRadioButton jRadioButton2;
private JRadioButton jRadioButton6;
private JScrollPane jScrollPane1;
private JSpinner jSpinner1;
private JSpinner jSpinner2;
private JTabbedPane jTabbedPane1;
private JTextArea jTextArea1;
private JTextField jTextField1;
private JRadioButton jYes;
private JRadioButton rb1;
private JRadioButton rb2;
private JRadioButton rb3;
private JTextField txt;
private JSpinner spp2;
private Date dat;
private Date dat2;
private JLabel jLabel8;
private JTextArea jTextArea8;
private JTextField jTextField8;
private JButton jButton8;  
private JButton jButton82; 
private JButton jButton83;   
private JLabel jLabel82;




public MyJFrame(){
    JPanel p1=new JPanel();

    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    //User r;
    jRadioButton6 = new JRadioButton();
    jPanel5 = new JPanel();
    buttonGroup1 = new ButtonGroup();
    buttonGroup2 = new ButtonGroup();

    jLabel1 = new JLabel("Event Title:");
    JTextField tit = new JTextField();       
    jLabel2 = new JLabel("Event Description:");
    JTextArea des = new JTextArea();        
    jScrollPane1 = new JScrollPane();
    jLabel3 = new JLabel("Start date:");
    jLabel4 = new JLabel("End date:");

    Date date = new Date();
    SpinnerDateModel sp = new SpinnerDateModel(date,null,null,Calendar.DAY_OF_MONTH);
    JSpinner spp = new JSpinner(sp);

    jSpinner1 = new JSpinner(sp);
    JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner1,"dd/mm/yyyy");
    jSpinner1.setEditor(de);



    Date date1 = new Date();
    SpinnerDateModel sp1 = new SpinnerDateModel       (date1,null,null,Calendar.HOUR_OF_DAY);
    JSpinner spp3 = new JSpinner(sp1);
    spp2 = new javax.swing.JSpinner(sp1);
    de2 = new JSpinner.DateEditor(spp2,"dd/mm/yyyy");
    spp2.setEditor(de2);

    jLabel5 = new JLabel("Is it a Recurrent event?");
    jYes = new JRadioButton("Yes");
    jRadioButton2 = new JRadioButton("No");
    buttonGroup1 = new ButtonGroup();
    buttonGroup2 = new ButtonGroup();        
    buttonGroup2.add(jYes);
    buttonGroup2.add(jRadioButton2);

    rb1 = new JRadioButton("Daily");
    rb2 = new JRadioButton("Weekly");
    rb3 = new JRadioButton("Monthly");
    buttonGroup1.add(rb1);
    buttonGroup1.add(rb2);
    buttonGroup1.add(rb3);
    dur = new JLabel("Duration:");
    txt = new JTextField();
    jButton1 = new JButton("Add Event");
    jTabbedPane1 = new JTabbedPane();
    jPanel1 = new JPanel();
    jPanel3 = new JPanel();
    jPanel4 = new JPanel();
    jPanel2 = new JPanel();
    jLabel6 = new JLabel();
    jLabel8 = new JLabel("Event ID:");
    jTextArea8 = new JTextArea(); 
    jTextField8= new JTextField();
    jButton8=new JButton("Find");
    jButton82=new JButton("Cancel"); 
    jButton83=new JButton("Save Agenda");
    jLabel82 = new JLabel();


    Container contentpane;
    contentpane=getContentPane();
    setSize(600,800);
    setLocation(600,140);
    JPanel tt=new JPanel();
    setDefaultCloseOperation(EXIT_ON_CLOSE);        
    tt.setLayout(new BorderLayout());
    getContentPane().add(tt);
    JTabbedPane tabbedPAne=new JTabbedPane();
    tabbedPAne.add("find event",p1);
    tabbedPAne.add("add event",p2);
    tabbedPAne.add("reminders",p3);
    tabbedPAne.add("save&load",p4);
    tt.add(tabbedPAne,BorderLayout.CENTER);
    p2.setLayout(null);
    jLabel1.setBounds(150,80,90,20);
    p2.add(jLabel1);
    tit.setBounds(240,80,170,20);
    p2.add(tit);
    jLabel2.setBounds(100,190,140,20);
    p2.add(jLabel2);
    des.setBounds(240,140,170,140);
    p2.add(des);
    jLabel3.setBounds(150,340,90,20);
    p2.add(jLabel3);
    jSpinner1.setBounds(240,340,160,20);
    p2.add(jSpinner1);
    jLabel4.setBounds(150,390,90,20);
    p2.add(jLabel4);
    spp2.setBounds(240,390,160,20);
    p2.add(spp2);        
    jLabel5.setBounds(120,450,190,20);
    p2.add(jLabel5);
    jYes.setBounds(300,450,50,20);
    jRadioButton2.setBounds(360,450,90,20);
    p2.add(jYes);
    p2.add(jRadioButton2);
    rb1.setBounds(140,500,60,20);
    rb2.setBounds(240,500,70,20);
    rb3.setBounds(340,500,70,20);
    p2.add(rb1);
    p2.add(rb2);
    p2.add(rb3);
    dur.setBounds(155,550,60,20);
    p2.add(dur);
    txt.setBounds(240,550,170,20);
    p2.add(txt);
    rb1.setEnabled(false);
    rb2.setEnabled(false);
    rb3.setEnabled(false);
    dur.setEnabled(false);
    txt.setEnabled(false);
    jButton1.setBounds(250,630,100,20);
    p2.add(jButton1);
    jLabel8.setBounds(90,30,60,20);
    p1.add(jLabel8);
    jTextField8.setBounds(80,30,45,20);
    p1.add(jTextArea8);
    jTextArea8.setBounds(100,80,130,70);
    p1.add(jTextArea8);
    jButton83.setBounds(30,510,50,20);
    p1.add(jButton83);
    jButton8.setBounds(100,45,50,20);
    p1.add(jButton8);
    jButton82.setBounds(90,45,50,20);
    p1.add(jButton82);
    jLabel82.setBounds(230,250,100,300);
    p1.add(jLabel82);

    setTitle("Tests' Planner");
    jYes.addActionListener(this); 
    jButton1.addActionListener(this); }




@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}
public static void main(String[] args) {
    new MyJFrame().setVisible(true);;
}   

}