java - 在 JScrollPane 内的 JPanel 内包装 JLabel

标签 java swing jpanel jscrollpane jlabel

我的 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/

相关文章:

java - Spring启动测试期待ConstraintViolationException但收到JpaSystemException

java - 运行外部 JUnit Jar 的 Spring 导致 java.lang.Exception : No runnable methods

swing - 是否调用Later()

java - 如何在Java中设置背景颜色和drawString而不取消另一个?

java - Google Drive API - 添加和更新权限

java - 如何在 Java 中有效地管理文件系统上的文件?

java - JTextPane - 不使用 JScrollPane 滚动可见文本

java - 类转换异常 - 鼠标事件

java - 如何翻转使用 Graphics 或 Graphics2D 创建的点相对于中心点的位置

java - 如何使 JOptionPane 位于全屏窗口之上?