0

When i go down and then return up, the button disappears. I don't know why..Is there any advice on this? I tried search in google/stackoverflow but I have not found answer. Anyway I don't have any ideas for this question, I tried everything

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(...) has no effect since you are using a layout manager. – FredK Apr 22 '16 at 17:10
  • I mean, the best answer is: use this code in IDE and look button, when scroll down and scroll up the button is invisible – Hundasse Apr 22 '16 at 17:11

1 Answers1

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);
  1. You set the content pane to have a BorderLayout.
  2. Then you add the "button" to the contentPane and the "jsp" to the contentPane. 3.The problem is you don't specify a constraint so both components are added to the "CENTER". However, only a single component can be displayed in the "CENTER" so only the scrollpane is displayed.
  3. However, the scrollpane contains an empty panel to there is nothing to see.

Not really sure what you are trying to do but you can try something like:

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

Don't:

  1. use a null layout
  2. use setBounds()
  3. use setPreferredSize()
camickr
  • 321,443
  • 19
  • 166
  • 288