如何在框架上滚动时使JButton随之移动?

huangapple go评论76阅读模式
英文:

How to have a JButton that moves as I scroll on the frame?

问题

我有一个面板,我已经在我的框架中将其设为可滚动。

我需要做的是在右下角添加一个按钮,即使滚动时也保持固定。

我对Java Swing不太熟悉,所以对我能得到的所有帮助都会感激不尽。

mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel

//使主面板可滚动
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);

// JFrame的设置
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
英文:

I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.

mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel

//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);

// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

</details>


# 答案1
**得分**: 2

我会使用嵌套的面板,其中外部面板采用 `BorderLayout`。然后使用一个采用 `FlowLayout` 的面板,将其对齐到 `FlowLayout.RIGHT`,并将按钮放在其中。

```java
public class Example extends JFrame {
    public Example() {
        super("");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        JTextArea textArea = new JTextArea(10000, 0);
        JScrollPane scrollPane = new JScrollPane(textArea);

        add(scrollPane, BorderLayout.CENTER);

        JButton button = new JButton("button");

        JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panelWithButton.add(button);
        add(panelWithButton, BorderLayout.PAGE_END);

        setLocationByPlatform(true);
        pack();
        setSize(600, 600);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new Example().setVisible(true);
        });
    }
}

结果:

如何在框架上滚动时使JButton随之移动?

英文:

I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.

public class Example extends JFrame {
	public Example() {
		super(&quot;&quot;);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout(new BorderLayout());

		JTextArea textArea = new JTextArea(10000, 0);
		JScrollPane scrollPane = new JScrollPane(textArea);

		add(scrollPane, BorderLayout.CENTER);

		JButton button = new JButton(&quot;button&quot;);

		JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		panelWithButton.add(button);
		add(panelWithButton, BorderLayout.PAGE_END);

		setLocationByPlatform(true);
		pack();
		setSize(600, 600);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -&gt; {
			new Example().setVisible(true);
		});
	}
}

Result:

如何在框架上滚动时使JButton随之移动?

答案2

得分: 1

我会选择使用BoxLayout。在metaPanel中添加另一个面板(scrollingPanel),然后在其中放置你的滚动面板,然后再添加一个按钮。不再使用scrollingPanel作为contentPane,而是使用metaPanel。示例(示例可行,但你需要修改它以使界面看起来更好):

JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);

JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("按钮"));

// JFrame的设置
frame = new JFrame("仓库模拟器");
frame.setContentPane(metaPanel); // 在这里放置metaPanel
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
英文:

I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):

    JPanel mainPanel = new JPanel();
    JScrollPane scrollPane = new JScrollPane(mainPanel);
    scrollPane.setViewportView(mainPanel);

    JPanel metaPanel = new JPanel();
    BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
    metaPanel.setLayout(boxlayout);
    metaPanel.add(scrollPane);
    metaPanel.add(new JButton(&quot;button&quot;));

    // Settings for JFrame
    frame = new JFrame(&quot;Warehouse Simulator&quot;);
    frame.setContentPane(metaPanel); // Put metaPanel here
    frame.setSize(500, 300);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);

huangapple
  • 本文由 发表于 2020年3月15日 22:16:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/60693842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定