简体   繁体   中英

Java: In AbsoluteLayout, JButtons do no show up until the mouse enters it

I have switched my Layout to a AbsoluteLayout and everything is in place where it needs to be, but when I run my program, my JButtons are invisible until I mouse over them. What should I change so they are always visible, mouse entered or not?

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Cursor;

public class Library extends JFrame implements ActionListener, MouseListener {

private JFrame jf1;
private JPanel jp1;
private JTextField jtf1;
private JTextField jtf2;
private JTextField jtf3;
private JButton jb1;
private JButton jb2;
private JButton jb3;

public Library() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(Exception ee) {
        ee.printStackTrace();
    }
    JFrame.setDefaultLookAndFeelDecorated(false);
    jf1 = new JFrame("Library");
    jf1.setVisible(true);
    jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf1.setSize(1080, 900);
    jf1.setResizable(true);
    jf1.getContentPane().setLayout(null);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);

    jp1 = (JPanel) jf1.getContentPane();

    jb1 = new JButton("Genre");
    jb1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    jb1.setBounds(345, 11, 150, 60);
    jb1.addActionListener(this);

    jb2 = new JButton("Author");
    jb2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    jb2.setBounds(494, 11, 150, 60);
    jb2.addActionListener(this);

    jb3 = new JButton("Title");
    jb3.setDefaultCapable(false);
    jb3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    jb3.setBounds(643, 11, 150, 60);
    jb3.addActionListener(this);

    jtf2 = new JTextField("Enter Text");
    jtf2.setBounds(445, 292, 200, 20);
    jtf2.addMouseListener(this);
    jtf2.setVisible(false);

    jtf3 = new JTextField("Enter Text");
    jtf3.setBounds(671, 351, 200, 20);
    jtf3.addMouseListener(this);
    jtf3.setVisible(false);

    jp1.add(jtf2);

    jtf1 = new JTextField("Enter Text");
    jtf1.setBounds(236, 230, 200, 20);
    jtf1.addMouseListener(this);
    jtf1.setVisible(false);

    jp1.add(jtf1);
    jp1.add(jtf3);
    jp1.add(jb1);
    jp1.add(jb2);
    jp1.add(jb3);

    jf1.validate();
}

@Override
public void actionPerformed(ActionEvent e) {
    Object code = e.getSource();
    if (code == jb1) {
        jtf1.setVisible(true);
        jp1.validate();
    }
    else if (code == jb2) {
        jtf2.setVisible(true);
        jp1.validate();
    }
    else if (code == jb3) {
        jtf3.setVisible(true);
        jp1.validate();
    }
}

public static void main(String[] args) {
    Library shoe = new Library();
}

@Override
public void mouseClicked(MouseEvent eee) {
    Object mouseCode = eee.getSource();
    if(mouseCode == jtf1) {
        jtf1.selectAll();
    } 
    if(mouseCode == jtf2) {
        jtf2.selectAll();
    }
    if(mouseCode == jtf3) {
        jtf3.selectAll();
    }
}

@Override
public void mouseEntered(MouseEvent eee) {}

@Override
public void mouseExited(MouseEvent eee) {}

@Override
public void mousePressed(MouseEvent eee) {}

@Override
public void mouseReleased(MouseEvent eee) {}

}

There are multiple errors in your program:

  1. You're using AbsoluteLayout which in the end it's still a null layout , see Null layout is evil and Why is it frowned upon to use a null layout in Swing? . While absolute positioning might seem like the fastest and easiest way to make complex GUI's for Swing beginners, the more you advance in your program the more errors you'll get due to this. Instead use a Layout Manager or combinations of them along with Empty borders for extra spacing if needed.

  2. You're creating a JFrame object and extending JFrame that, in other words, makes your class a JFrame ! There's no need to extend JFrame if you really need to extend something extend a JPanel which can be added later into other components while JFrame can't.

  3. You're making your JFrame visible before you have added all your components to it, that's the reason for your error, and probably the 1st one too. You should make it visible only in the end, after you have added all your components to it.

Set the JFrame visible ( jf1.setVisible(true); ) after you add all components. (End of your constructor)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM