0
當我走下然後返回時,按鈕消失。我不知道爲什麼......有沒有關於這方面的任何建議?我試過在谷歌/ stackoverflow搜索,但我還沒有找到答案。反正我沒有對這個問題的任何想法,我什麼都試過按鈕變爲不可見
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class GenerateExam extends JFrame {
private JPanel contentPane;
private JButton button;
public GenerateExam() {
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 800, 600);
button = new JButton("Click me!");
button.setBounds(50, 100, 100, 200);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.add(button);
setContentPane(contentPane);
JPanel container = new JPanel();
JScrollPane jsp = new JScrollPane(container);
jsp.getVerticalScrollBar().setUnitIncrement(16);
container.setPreferredSize(new Dimension(750, 4000));
container.setLayout(null);
getContentPane().add(jsp);
centerFrame();
}
private void centerFrame() {
Dimension windowSize = getSize();
GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();
Point centerPoint = g.getCenterPoint();
int dx = centerPoint.x - windowSize.width/2;
int dy = centerPoint.y - windowSize.height/2;
setLocation(dx, dy);
}
public static void main(String[] args) {
GenerateExam exam = new GenerateExam();
exam.setVisible(true);
}
}
由於使用佈局管理器,button.setBounds(...)不起作用。 – FredK
我的意思是,最好的答案是:在IDE中使用此代碼並查看按鈕,向下滾動並向上滾動按鈕不可見 – Hundasse