gpt4 book ai didi

java - 添加 JScrollPane 以避免组件变形

转载 作者:行者123 更新时间:2023-12-01 11:12:20 24 4
gpt4 key购买 nike

我正在尝试制作一个可变大小的界面。然而,目前,如果所有组件的大小都大于窗口的尺寸,那么这些组件就会开始
扭曲!因此,我想添加一个JScrollPane,当窗口大小不再足够时显示,以避免这种扭曲。我刚开始添加一个 JScrollPane (如下面的代码所示),但它似乎不起作用,我不知道现在要更改什么。

JScrollPane myScrollPane = new JScrollPane();
myWindow.getContentPane().add(myScrollPane, BorderLayout.CENTER);

编辑1:这是我尝试过的一个简单示例:

static void one(String[] ari, JLabel sess_nb, Box boxy, long[] res){
int p;
int length = ari.length;
Border cadre = BorderFactory.createLineBorder(Color.black);
Font police = new Font("Monospaced 13", Font.BOLD, 18);
Font police1 = new Font("Monospaced 13", Font.BOLD, 15);
Font font = new Font("Monospaced 13", Font.ITALIC, 13);
for (p=0;p<length;p++){
Box boxz = Box.createVerticalBox();
JLabel Rate = new JLabel("Rating Group "+r);
JLabel rg_reser = new JLabel();
JLabel switsh = new JLabel("1");
JLabel reser = new JLabel(); //in this label the resrvation for each update and each rating group will be stocked
JLabel consump = new JLabel(); //in this label will be stocked the consumption
//----------------------------------------------------

//choice of MB or kB
JComboBox combo = new JComboBox();
combo.addItem("Megabytes");
combo.addItem("Bytes");
combo.addItem("kilobytes");
combo.addItem("Gigabytes");
JLabel upload = new JLabel("Upload consumption:");
JTextField upload_entry = new JTextField("7.5");
JLabel download = new JLabel("Download consumption:");
JTextField download_entry = new JTextField("7.5");
JTextField total_entry = new JTextField();
JLabel total = new JLabel("Total consumption:");
JButton rg = new JButton("Next");
JLabel update = new JLabel("Result here");
boxz.add(Rate);
boxz.add(total);
boxz.add(total_entry);
boxz.add(upload);
boxz.add(upload_entry);
boxz.add(download);
boxz.add(download_entry);
boxz.add(combo);
boxz.add(rg);
boxz.add(update);
boxy.add(boxz);
scrollPane1.add(boxy);
}

编辑2:这是一个新的测试代码,但它也不起作用:

public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));

scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
content.add(imad);
content.add(textArea);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);
}

出现一个小的白色恐惧(我认为这是滚动条或滚动面板?)并且窗口中的组件超出了窗口大小,但当我尝试滚动(使用鼠标或通过点击白色恐惧)时没有任何反应)。我不知道出了什么问题。

最佳答案

不要使用scrollPanel.add(boxy)-您需要将boxy添加到scrollPanel的视口(viewport),而不是scrollPanel本身。请改用 scrollPanel.setViewportView(boxy),或者最好在创建 boxy 后创建 ScrollPanel,并使用采用 Component 参数的构造函数。

关于java - 添加 JScrollPane 以避免组件变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209353/

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