代码之家  ›  专栏  ›  技术社区  ›  sagschni

无法查看JComboBox或JCheckBox

  •  1
  • sagschni  · 技术社区  · 10 年前

    我想用Swing Forms制作一个Java应用程序。但我有个问题。我可以看到所有文本框、所有按钮和标签。但没有办法看到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 回复  |  直到 10 年前
        1
  •  2
  •   MadProgrammer    10 年前

    问题在哪里?

    container.setLayout(null); 是你的问题的主要根源,在你停止使用它之前,它会一直给你带来痛苦。

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

    我想用Swing Forms制作一个Java应用程序

    然后是学习如何使用布局管理器的时候了,看看 Laying Out Components Within a Container 有关详细信息