无法看到JComboBox或JCheckBox

时间:2014-07-22 08:28:00

标签: java swing jcombobox layout-manager null-layout-manager

我想用Swing Forms创建一个Java-Aplication。但我有一个问题。我可以看到所有文本框,所有按钮都是标签。但是没有办法看到JComboBox。与JCheckBox相同。问题出在哪里?

import javax.swing.*;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.io.File;
import java.util.Map;
import java.util.Vector;


public class Testtool{
    private JFrame frame;
    private JTextArea taAusgabe;
    private JFileChooser file1;
    private JFileChooser file2;
    private JComboBox cbFormate;
    private DefaultComboBoxModel cbmFormate;

    public JButton btn1;
    public JButton btn2;
    public JButton btn3;
    public JButton btnClear;
    public JLabel laLoading;

    private boolean debug = false;

    private static Testtool window;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    window = new Testtool();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Testtool() throws InterruptedException {
        Vector<String> formate = new Vector<String>();
        formate.add("Format");
        formate.add("1");
        formate.add("2");

        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setBounds(100, 100, 720, 510);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setResizable(false);            

        Container container = frame.getContentPane();

        Font header = new Font("Serif", Font.BOLD, 22);
        Map attributes = header.getAttributes();
        attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);

        Font normal = new Font("Serif", Font.BOLD, 16);

        JLabel laHeader = new JLabel();
        laHeader.setHorizontalAlignment(SwingConstants.CENTER);
        laHeader.setText("Testtool für Transformationsskripte");
        laHeader.setFont(header.deriveFont(attributes));
        laHeader.setSize(740, 40);
        laHeader.setLocation(0, 10);
        container.add(laHeader);

        JLabel laFormat = new JLabel();
        laFormat.setText("Format:");
        laFormat.setFont(normal);
        laFormat.setSize(160, 20);
        laFormat.setLocation(10, 80);
        container.add(laFormat);

        cbFormate = new JComboBox();
        cbmFormate = new DefaultComboBoxModel(formate);
        cbFormate.setModel(cbmFormate);
        cbFormate.setLocation(60, 80);
        container.add(cbFormate);

        JLabel la1= new JLabel();
        la1.setText("Messwertdatei:");
        la1.setFont(normal);
        la1.setSize(160, 20);
        la1.setLocation(10, 110);
        container.add(la1);


        final JLabel la2 = new JLabel();
        la2 .setSize(410, 20);
        la2 .setLocation(300, 110);
        container.add(la2 );

        btn1= new JButton();
        btn1.setText("Auswählen");
        btn1.setSize(100, 20);
        btn1.setLocation(180, 110);
        btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                file1= new JFileChooser();
                int returnVal1 = file1.showOpenDialog(null);
                if (returnVal1 == JFileChooser.APPROVE_OPTION) {
                    File f1= file1.getSelectedFile();
                    la2 .setText(f1.getAbsolutePath());
                    addLogLine(f1.getAbsolutePath() + " ausgewählt", false);
                }
            }
        });
        container.add(btn1);


        final JLabel la3= new JLabel();
        la3.setText("Transformationsskript:");
        la3.setFont(normal);
        la3.setSize(160, 20);
        la3.setLocation(10, 140);
        container.add(la3);

        final JLabel la4= new JLabel();
        la4.setSize(410, 20);
        la4.setLocation(300, 140);
        container.add(la4);

        btn2 = new JButton();
        btn2 .setText("Auswählen");
        btn2 .setSize(100, 20);
        btn2 .setLocation(180, 140);
        btn2 .addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                file2= new JFileChooser();
                int returnVal2 = file2.showOpenDialog(null);
                if (returnVal2 == JFileChooser.APPROVE_OPTION) {
                    File f2= f2.getSelectedFile();
                    la4.setText(f2.getAbsolutePath());
                    addLogLine(f2.getAbsolutePath() + " ausgewählt", false);
                }
            }
        });
        container.add(btn2 );

        btn3 = new JButton();
        btn3.setText("go");
        btn3.setSize(270, 20);
        btn3.setLocation(10, 170);
        btn3.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                start();
            }

            public void mouseClicked(MouseEvent arg0) {

            }
        });
        container.add(btn3 );

        ImageIcon loading = new ImageIcon(getClass().getClassLoader().getResource("ajax-loader.gif"));
        laLoading = new JLabel();
        laLoading.setVisible(false);
        laLoading.setIcon(loading);
        laLoading.setSize(20, 20);
        laLoading.setLocation(300, 170);
        container.add(laLoading);

        JLabel laAusgabe = new JLabel();
        laAusgabe.setText("Ausgabe:");
        laAusgabe.setFont(normal);
        laAusgabe.setSize(100, 20);
        laAusgabe.setLocation(10, 200);
        container.add(laAusgabe);

        btnClear = new JButton();
        btnClear.setText("Ausgabe löschen");
        btnClear.setSize(160, 20);
        btnClear.setLocation(120, 200);
        btnClear.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                taAusgabe.setText("");
            }
        });
        container.add(btnClear);

        final JButton btnDebug = new JButton();
        btnDebug.setText("Debugging: AUS");
        btnDebug.setSize(160, 20);
        btnDebug.setLocation(540, 200);
        btnDebug.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

            }
        });
        container.add(btnDebug);

        taAusgabe = new JTextArea();
        DefaultCaret caret = (DefaultCaret)taAusgabe.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        taAusgabe.setSize(690, 250);
        taAusgabe.setBackground(container.getBackground());
        taAusgabe.setEditable(false);

        JScrollPane scroll = new JScrollPane(taAusgabe, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setLocation(10, 220);
        scroll.setSize(690, 250);
        scroll.setAutoscrolls(true);
            container.add(scroll);       

container.setLayout(null);  


    }

    public void addLogLine(String logline, boolean debug){
        if (this.debug){
            taAusgabe.append(logline + '\n');
        }
        else{
            if(!debug){
                taAusgabe.append(logline + '\n');
            }
        }
    }

    public void stop(){
        btnClear.setEnabled(true);
        btn1.setEnabled(true);
        btn2 .setEnabled(true);
        btn3.setEnabled(true);
        laLoading.setVisible(false);
    }

    public void start(){
        btnClear.setEnabled(false);
        btn1.setEnabled(false);
        btn2 .setEnabled(false);
        btn3.setEnabled(false);
        laLoading.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:2)

  

问题出在哪里?

container.setLayout(null);是你问题的主要原因,在你停止使用之前会一直持续疼痛。

您无法控制用于确定组件在不同平台和操作系统上所需空间的度量标准。像素完美布局是现代UI开发中的一种幻觉。即使在相同的操作系统之间,您最终也会在字体类型和渲染管道方面产生足够的差异,从而使界面变得无用。

  

我想用Swing Forms创建一个Java-Aplication

然后是时候学习如何使用布局管理器了,详细了解Laying Out Components Within a Container