我轉我的Layout
到AbsoluteLayout
,一切都在地方,它需要的,但是當我運行我的程序,我JButtons
是看不見的,直到我把鼠標放在它們上我應該改變什麼,以便它們始終可見,鼠標是否進入?的Java:在AbsoluteLayout,Jbutton將做無顯示,直到鼠標進入它
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) {}
}
謝謝!你可以向我解釋爲什麼這樣做,所以我有知識/理解,我不只是記住在線代碼? – jholsch29
下面是它的一個很好的解釋:http://stackoverflow.com/questions/10790457/why-shouldnt-i-call-setvisibletrue-before-adding-components – pL4Gu33