当我下降然后返回时,按钮消失。我不知道为什么......对此我有什么建议吗?我尝试在google / 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);
}
}
答案 0 :(得分:1)
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);
不确定你要做什么,但你可以尝试类似的事情:
JPanel contentPane = new JPanel( new BorderLayout() );
setContentPane( contentPane );
JButton button = new JButton("Click Me");
add(button, BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane( new JTextArea(10, 40) );
add(scrollPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null); // this will center the frame
不要: