带有null布局的JFrame不显示任何内容



我的JFramenull布局不会显示任何东西....我尝试将组件添加到JPanel,然后将其设置为框架的内容窗格。最近(如下面的代码所示),我尝试直接添加到框架中。我用的是Java 8。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Electog extends JFrame implements MouseListener,      MouseMotionListener {
     BlankArea blankArea;
     JTextArea textArea;
static JFrame frame;
public static void main(String[] args) {
    JFrame frame = new Electog();
    frame.setTitle("Electog"); 
    frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    frame.setVisible(true);
}
public Electog() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    JRadioButton selectButton = new JRadioButton("Select");
    selectButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    selectButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    selectButton.setSelected(true);
    JRadioButton removeButton = new JRadioButton("Remove");
    removeButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    removeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton openButton = new JRadioButton("Open");
    openButton.addKeyListener(new KeyListener() {
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    openButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });

    JRadioButton saveButton = new JRadioButton("Save");
    saveButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton wireButton = new JRadioButton("Wiring");
    wireButton.setMnemonic(KeyEvent.VK_W);
    wireButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    wireButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton subcircuitButton = new JRadioButton("Subcircuit");
    subcircuitButton.setMnemonic(KeyEvent.VK_C);
    subcircuitButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    subcircuitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton ANDButton = new JRadioButton("AND Gate");
    ANDButton.setMnemonic(KeyEvent.VK_A);
    ANDButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    ANDButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton NANDButton = new JRadioButton("NAND Gate");
    NANDButton.setMnemonic(KeyEvent.VK_N);
    NANDButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    NANDButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton ORButton = new JRadioButton("OR Gate");
    ORButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    ORButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton groundButton = new JRadioButton("Ground");
    groundButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    groundButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    JRadioButton powerButton = new JRadioButton("Power");
    powerButton.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent e) {
            handleAction(e);
        }
        @Override
        public void keyTyped(KeyEvent e) {
        }
        @Override 
        public void keyPressed(KeyEvent e) {
            handleAction(e);
        }
    });
    powerButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleAction(e);
        }
        public void actionEvent(ActionEvent e) {
            handleAction(e);
        }
    });
    ButtonGroup group = new ButtonGroup();
    group.add(selectButton);
    group.add(removeButton);
    group.add(openButton);
    group.add(saveButton);
    group.add(wireButton);
    group.add(subcircuitButton);
    group.add(ANDButton);
    group.add(NANDButton);
    group.add(ORButton);
    group.add(groundButton);
    group.add(powerButton);
    //menu buttons, still need more features and zoom/move simulation screen
    add(selectButton);
    add(removeButton);
    add(openButton);
    add(saveButton);
    add(wireButton);
    add(subcircuitButton);
    add(ANDButton);
    add(NANDButton);
    add(ORButton);
    add(groundButton);
    add(powerButton);
    selectButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, 0, 0); //11 is number of buttons as far as now
    removeButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 0);
    openButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (2.0/11.0)), 0);
    saveButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (3.0/11.0)), 0);
    wireButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (4.0/11.0)), 0);
    subcircuitButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (5.0/11.0)), 0);
    ANDButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/11)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (6.0/11.0)), 0);
    NANDButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (7.0/11.0)), 0);
    ORButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (8.0/11.0)), 0);
    groundButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (9.0/11.0)), 0);
    powerButton.setBounds((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1.0/11.0)), 50, (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (10.0/11.0)), 0);
    //subcircuit buttons
    JScrollPane ScrollPane = new JScrollPane();
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setSize(new Dimension((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/5)), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50)));
    ScrollPane.setViewportView(buttonPanel);
    add(ScrollPane);
    ScrollPane.setBounds((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/5)), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50), 0, 50);
    ScrollPane.updateUI();
    blankArea = new BlankArea(Color.WHITE);
    add(blankArea);
    blankArea.addMouseListener(this);
    blankArea.addMouseMotionListener(this);
    blankArea.setBounds((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (4/5)), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50), (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (1/5)), 50);
    addMouseListener(this);
    addMouseMotionListener(this);
    setPreferredSize(new Dimension((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() * (4/5)),(int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 50)));
}
public void handleAction(KeyEvent e) {
    if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 && (e.getKeyChar() == 'S' || e.getKeyChar() == 's')) {
        try {
            //saveCurrentFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 && (e.getKeyChar() == 'O' || e.getKeyChar() == 'o')) {
        try {
            //openFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'A' || e.getKeyChar() == 'a')) {
        //selectAll();
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'A' || e.getKeyChar() == 'a')) {  
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'N' || e.getKeyChar() == 'n')) {  
    } else if ((e.getModifiers() & KeyEvent.CTRL_MASK) == 0 && (e.getKeyChar() == 'O' || e.getKeyChar() == 'o')) {  
    }
}
public void handleAction(ActionEvent e) {
    if (e.getActionCommand() == "Save") {
        try {
            //saveCurrentFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if (e.getActionCommand() == "Open") {
        try {
            //openFile();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(frame, ex.getStackTrace());
        }
    } else if (e.getActionCommand() == "AND Gate") {    
    } else if (e.getActionCommand() == "NAND Gate") {   
    } else if (e.getActionCommand() == "OR Gate") { 
    } else if (e.getActionCommand() == "Select") {  
    } else if (e.getActionCommand() == "Ground") {  
    } else if (e.getActionCommand() == "Power") {   
    } else if (e.getActionCommand() == "Remove") {  
    } else if (e.getActionCommand() == "Subcircuit") {  
    } else if (e.getActionCommand() == "Wiring") {  
    }
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
}

每个java.​awt.​Component都有一个setBounds(int x, int y, int width, int height)方法用于确定其在容器中的位置。

相关内容

  • 没有找到相关文章

最新更新