我试图通过jpanel在我的jframe上添加jscrollpane,但它不起作用。问题是,如果我不添加滚动条,我想在框架上显示的字符串太长,它将不会显示并将被切断。但是,当我添加滚动窗格时,jframe不显示所有字符串。要在窗口上“绘制”字符串,我使用的是setText()。这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.border.LineBorder;
public class LabelFrame extends JFrame {
private final JTextField urlString;
private final JButton loadButton;
String content;
JTextArea textArea = new JTextArea();
public LabelFrame() {
super("WebStalker");
setSize(600, 600);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setLayout(new FlowLayout());
urlString = new JTextField("https:Search",30);
loadButton = new JButton("Load");
JPanel panel = new JPanel();
JLabel label = new JLabel("URL");
panel.add(label);
panel.add(urlString);
panel.add(loadButton);
JScrollPane jp = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(jp);
this.add(panel);
pack(); // JFrame στο ελάχιστο μέγεθος με όλα τα περιεχόμενα
setLocationRelativeTo(null); //τοποθετεί στο κέντρο το παράθυρο
TextFieldHandler tHandler = new TextFieldHandler();
ButtonHandler bHandler = new ButtonHandler();
urlString.addActionListener(tHandler);
loadButton.addActionListener(bHandler);
urlString.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
urlString.setText("");
}
});
}
private class TextFieldHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event){
try {
content = URLReaderFinal.Reading(event.getActionCommand());
textArea.setText(content);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackgroung(UIManager.getColor("Label.background");
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
getContentPane().add(textArea, BorderLayout.CENTER);
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, "This url doesnt exist","Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
try {
content = URLReaderFinal.Reading(urlString.getText());
textArea.setText(content);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(UIManager.getColor("Label.background"));
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
getContentPane().add(textArea, BorderLayout.CENTER);
} catch (Exception e) {
System.out.println("Unable to load page");
JOptionPane.showMessageDialog(null, "Unable to load page","Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
答案 0 :(得分:4)
在你的按钮处理程序中,你正在重新添加一个JTextArea而没有它的JScrollPane:
getContentPane().add(textArea, BorderLayout.CENTER);
基本上是在你需要它时从JScrollPane中删除它 - 不要这样做,因为这将完全弄乱你并将从视图中删除JScrollPane。而是将JTextArea留在原处,不要尝试将组件重新添加到JFrame的contentPane,只需根据需要将文本添加到JTextArea。
另外,不要忘记提供JTextArea列和行属性,这可以通过使用带有两个整数的构造函数轻松完成。并将contentPane的布局保留为BorderLayout:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import javax.swing.border.LineBorder;
@SuppressWarnings("serial")
public class LabelFrame extends JFrame {
private static final int ROWS = 30;
private static final int COLS = 80;
private final JTextField urlString;
private final JButton loadButton;
String content;
JTextArea textArea = new JTextArea(ROWS, COLS);
public LabelFrame() {
super("WebStalker");
// setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setLayout(new FlowLayout()); // !! no
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setOpaque(false);
textArea.setEditable(false);
textArea.setFocusable(false);
textArea.setBackground(UIManager.getColor("Label.background"));
textArea.setFont(UIManager.getFont("Label.font"));
textArea.setBorder(UIManager.getBorder("Label.border"));
urlString = new JTextField("https:Search", 30);
loadButton = new JButton("Load");
JPanel panel = new JPanel();
JLabel label = new JLabel("URL");
panel.add(label);
panel.add(urlString);
panel.add(loadButton);
JScrollPane jp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// panel.add(jp);
this.add(panel, BorderLayout.PAGE_START);
add(jp);
pack(); // JFrame στο ελάχιστο μέγεθος με όλα τα περιεχόμενα
setLocationRelativeTo(null); // τοποθετεί στο κέντρο το παράθυρο
TextFieldHandler tHandler = new TextFieldHandler();
ButtonHandler bHandler = new ButtonHandler();
urlString.addActionListener(tHandler);
loadButton.addActionListener(bHandler);
urlString.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
urlString.setText("");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new LabelFrame().setVisible(true);
});
}
private class TextFieldHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
try {
content = URLReaderFinal.Reading(event.getActionCommand());
textArea.setText(content);
// !! getContentPane().add(textArea, BorderLayout.CENTER);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "This url doesnt exist", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
try {
content = URLReaderFinal.Reading(urlString.getText());
textArea.setText(content);
// getContentPane().add(textArea, BorderLayout.CENTER);
} catch (Exception e) {
System.out.println("Unable to load page");
JOptionPane.showMessageDialog(null, "Unable to load page", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
编辑:不要在JTextField上使用MouseListener。也许您想要使用FocusListener。
取而代之的是:
public class LabelFrame extends JFrame {
private static final int ROWS = 30;
private static final int COLS = 80;
private static final String HTTPS_SEARCH = "https:Search";
// .....
public LabelFrame() {
// ....
urlString = new JTextField(HTTPS_SEARCH, 30);
//.....
urlString.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
JTextField textField = (JTextField) e.getComponent();
String text = textField.getText();
if (text.equals(HTTPS_SEARCH)) {
textField.setText("");
}
}
});