框架/面板上的组件有问题吗?

时间:2014-05-27 13:58:24

标签: java jpanel

我需要为日期构建3个JcomboBox,要编写文本字段以及保存删除或打开已保存文件的按钮。我的问题是,我无法看到文本块,直到 我不用鼠标移动按钮它们也是不可见的。提前致谢。

    package q2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Panel extends JPanel{

    Hashtable<Date, String> dateTable = new Hashtable<Date, String>();

    String[] days = {"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
    String[] months = {"00","01","02","03","04","05","06","07","08","09","10","11","12"};
    String[] years = {"0000" , "2014" , "2015" , "2016" , "2017" , "2018"};


public Panel(){

    this.setLayout(new BorderLayout());
    // adding the components to the frame

    this.add(getCombo(), BorderLayout.NORTH);
    this.add(getButtons(), BorderLayout.SOUTH);
    this.add(getText(), BorderLayout.CENTER);

    repaint();
}

public JComboBox getDay()
{
    JComboBox day = new JComboBox(days);
    day.setSelectedIndex(0);
    return day;
}

public JComboBox getMonth()
{
    JComboBox month = new JComboBox(months);
    month.setSelectedIndex(0);
    return month;
}

public JComboBox getYear()
{
    JComboBox year = new JComboBox(years); 
    year.setSelectedIndex(0);
    return year;
}

protected JTextArea getText()
{
    JTextArea text = new JTextArea();
    Font textFont =  new Font("Miriam" , Font.BOLD , 16 );
    text.setFont(textFont);
    return text;
}

protected JComponent getButtons()
{
    JPanel buttons = new JPanel(new GridLayout(0,3));
    JButton save = new JButton("save reminder");
    JButton show = new JButton("show reminder");
    JButton delete = new JButton("delete reminder");

    buttons.add(save);
    buttons.add(show);
    buttons.add(delete);

    return buttons;
}

protected JComponent getCombo()
{
    JPanel forBoxes = new JPanel(new FlowLayout());
    forBoxes.add(getDay());
    forBoxes.add(getMonth());
    forBoxes.add(getYear());
    forBoxes.setPreferredSize(getMaximumSize());

    return forBoxes;
}

}





    package q2;
    import javax.swing.*;

    public class Tester {


    public static void main(String args[])
    {

        JFrame frame = new JFrame ("reminder");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setLocation(350, 200);
        Panel box = new Panel();
        frame.add(box);


        frame.setVisible(true);


    }
    }

1 个答案:

答案 0 :(得分:0)

我无法发表评论,因为我没有足够的声誉。所以我发布了一个答案。

通常当我用java swing编写程序时,如果我添加JButtons,我就看不到它们,直到我将鼠标放在他们的位置上。但是当我在一个可运行的.JAR文件中导出我的项目时,我的项目完美无缺,我可以看到Buttons而不会将鼠标移过它们。

所以我认为如果您将其导出到.JAR文件中,您会看到TextFieldButtons