gpt4 book ai didi

java - 在 JScrollPane 内的 JPanel 内包装 JLabel

转载 作者:行者123 更新时间:2023-11-29 07:06:14 25 4
gpt4 key购买 nike

我的 Java 有点生疏,所以请多多包涵。我的 GUI 类中有一个方法调用另一个返回 JList 的类文件。我遇到的问题是从 JList 获取文本,您可以在下面看到输出示例

package com.example.tests;

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.example.tests.IJ_runTestAFJ;
public class GUI_v2 extends JFrame
{
private static final long serialVersionUID = 1L;
IJ_CommonSetup setup = new IJ_CommonSetup();


Container c;
JPanel panel;
JScrollPane userScrollPane, errorScrollPane, sysScrollPane;
JTextArea tfUserError, tfSysError;

private JButton resetButton;
public JList<String> errorList;


GUI_v2()
{
resetButton = new JButton();
resetButton.setText("Click to populate TextArea");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//test.runTest_Login(stUserName,stPwd);
updatePanel();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});

panel = new JPanel();

tfSysError = new JTextArea(10,33);
tfSysError.setLineWrap(true);
tfSysError.setEditable(false);
tfSysError.setWrapStyleWord(false);
sysScrollPane = new JScrollPane(tfSysError);
sysScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));

panel.add(sysScrollPane);
panel.add(resetButton);

c = getContentPane();
c.add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(400,250); //width, height
setLocation(600,0);
setResizable(false);
validate();
}//close GUI

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
EventQueue.invokeLater(new Runnable() {

public void run() {
new GUI_v2().setVisible(true);
}
});
}

public void updatePanel()
{
errorList = new JList<String>();
errorList = setup.getErrorJList();
tfSysError.append(errorList.getComponent(1).toString());
validate();
}


}// end on class

IJ_CommonSetup.java

package com.example.tests;

import javax.swing.JLabel;
import javax.swing.JList;
public class IJ_CommonSetup{
/**
*
*/

public static String stError = new String();
public static JList<String> stJListError = new JList<String>();


public JList<String> getErrorJList(){
String error1 = new String("TestTestTestTestTestTestTestTestTestTestTestTestTestTest ");
String error2 = new String("ApplesApplesApplesApplesApplesApplesApplesApplesApplesApples ");
JLabel newError1 = new JLabel();
newError1.setText(error1);
JLabel newError2 = new JLabel(error2);
stJListError.add(newError1);
stJListError.add(newError2);
return stJListError;
}
}

最佳答案

im having some trouble getting labels to wrap inside a panel that's inside a Scrollpane. At the moment if the string thats added to the label is long it is aligned to the left which is fine but the label stretches outside the panel cutting off the end of the string.

  • JScrollPane

    中使用JTextArea(int, int)
  • setEditable(false) 用于 JTextArea

而不是 JLabel 添加到 JPanel(在 JScrollPane 中)

关于java - 在 JScrollPane 内的 JPanel 内包装 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19445442/

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