gpt4 book ai didi

Java - 打开 JScrollPane,并将 JScrollBar 设置为右侧

转载 作者:行者123 更新时间:2023-12-01 18:44:18 25 4
gpt4 key购买 nike

我正在尝试加载一个 JScrollPane,并将水平 JScrollBar 设置为一直向右,即如下图所示:

/image/02BKi.png

但是,无论我做什么,我最终都会得到这个图像:

/image/Eh50d.png

这是我的代码的相关片段

    JComponent graph = new TrendsForSuccLarge(wccScores, wccScoresTimes, graphStrings, maxScore, bgColour, iPadWidth);

JScrollPane graphScrollPane = new JScrollPane(graph, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

JScrollBar horizontal = graphScrollPane.getHorizontalScrollBar();
horizontal.setValue( horizontal.getMaximum() );

this.getContentPane().add(graphScrollPane, BorderLayout.CENTER);

对出了什么问题有什么想法吗?

提前致谢!

凯什

SSCCE

import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

class ForStackOverflow {

private static JFrame main;
//opens up a line chart on full screen

public static void main(String args[]) {

TempGraph tg = new TempGraph(main);
tg.setVisible(true);

}
}

class TempGraph extends JDialog {

private static final long serialVersionUID = 1L;

//opens up a line chart on full screen

public TempGraph(JFrame parent)
{
// load the frame
super(parent,true);
configureFrame();
}

private void configureFrame()
{

int iPadHeight = 644;
int iPadWidth = 981;

this.getContentPane().setLayout(new BorderLayout());

JComponent graph = new TrendsForSuccLarge2(iPadWidth);

JScrollPane graphScrollPane = new JScrollPane(graph, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

this.getContentPane().add(graphScrollPane, BorderLayout.CENTER);

JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());

JButton ok = new JButton("Done");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
next();
}
});
buttons.add(ok,BorderLayout.CENTER);
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
buttons.add(exit,BorderLayout.WEST);

this.getContentPane().add(buttons, BorderLayout.SOUTH);

this.pack();
this.setSize(this.getWidth(), 750);
this.setResizable(true);
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int widthFrame = iPadWidth;
int heightFrame = iPadHeight;
this.setSize(widthFrame, heightFrame);
setBounds((screenSize.width - this.getWidth()) / 2,
(screenSize.height - this.getHeight()) / 2,
getWidth(),
getHeight());
}

private void next()
{
this.setVisible(false);
}

}

class TrendsForSuccLarge2 extends JComponent {

// basic parameters
private static int PREF_W = 200;
private static final int PREF_H = 200;
private static final int BORDER_GAP = 10;
private static final int GRAPH_GAP = BORDER_GAP + 20;

private int graphWidth = 0;

private static final long serialVersionUID = 1L;

TrendsForSuccLarge2(int prefGraphWidth) {

graphWidth = prefGraphWidth;

paintComponent(null);
}

public void paint(Graphics g) {

int windowWidth = 2600;

PREF_W = windowWidth;

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.drawString("Hello", graphWidth/2, getHeight() - GRAPH_GAP);

g2.drawString("Hello1", graphWidth - GRAPH_GAP, getHeight() - GRAPH_GAP);

g2.drawString("Hello2", windowWidth - GRAPH_GAP - GRAPH_GAP, getHeight() - GRAPH_GAP);

// create x and y axes
g2.setColor(Color.black);
g2.drawLine(GRAPH_GAP, getHeight() - GRAPH_GAP, GRAPH_GAP, GRAPH_GAP);
g2.drawLine(GRAPH_GAP, getHeight() - GRAPH_GAP, windowWidth - GRAPH_GAP, getHeight() - GRAPH_GAP);

}

public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

}

最佳答案

the full code is here:

您发布的代码无法编译,因为我们无法访问您的所有自定义类。当您有问题时,发布正确的 SSCCE

horizontal.setValue( horizontal.getMaximum() ); 

您需要在对话框可见后调用该代码。

关于Java - 打开 JScrollPane,并将 JScrollBar 设置为右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com