JFrame添加滚动条
JFrame add scrollbar
我尝试了一些教程并查看了其他答案,但它似乎仍然没有帮助我,我想向这个 console-like JTextArea 添加一个滚动条,并保留 属性 因此每一行新的文本都会将其余的推上去。
这是目前的样子:
代码:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int width = 600;
int height = 400;
JFrame frame = new JFrame("ChanDown");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.LEFT) ;
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
JTextArea console = new JTextArea();
console.setBackground(Color.DARK_GRAY);
console.setForeground(Color.white);
console.setMargin(new Insets(0,10,10,10));
console.setLineWrap(true);
panel.add(console, BorderLayout.NORTH);
panel.add(textField, BorderLayout.CENTER);
frame.add(panel, BorderLayout.SOUTH);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setVisible(true);
将 JTextArea
添加到 JScrollPane
...
panel.add(new JScrollPane(console), BorderLayout.NORTH);
有关详细信息,请参阅 How to Use Scroll Panes
尝试这样的事情
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class ChanDown extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChanDown frame = new ChanDown();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChanDown() {
setForeground(Color.WHITE);
setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 381);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
textArea.append(textField.getText() + "\n");
textField.setText("");
textField.requestFocus();
}
}
});
textField.setColumns(10);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
);
textArea = new JTextArea();
textArea.setBackground(Color.DARK_GRAY);
textArea.setForeground(Color.WHITE);
textArea.setAlignmentY(Component.BOTTOM_ALIGNMENT);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
contentPane.setLayout(gl_contentPane);
}
}
我尝试了一些教程并查看了其他答案,但它似乎仍然没有帮助我,我想向这个 console-like JTextArea 添加一个滚动条,并保留 属性 因此每一行新的文本都会将其余的推上去。
这是目前的样子:
代码:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int width = 600;
int height = 400;
JFrame frame = new JFrame("ChanDown");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JTextField textField = new JTextField();
textField.setHorizontalAlignment(JTextField.LEFT) ;
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
JTextArea console = new JTextArea();
console.setBackground(Color.DARK_GRAY);
console.setForeground(Color.white);
console.setMargin(new Insets(0,10,10,10));
console.setLineWrap(true);
panel.add(console, BorderLayout.NORTH);
panel.add(textField, BorderLayout.CENTER);
frame.add(panel, BorderLayout.SOUTH);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setVisible(true);
将 JTextArea
添加到 JScrollPane
...
panel.add(new JScrollPane(console), BorderLayout.NORTH);
有关详细信息,请参阅 How to Use Scroll Panes
尝试这样的事情
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class ChanDown extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChanDown frame = new ChanDown();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChanDown() {
setForeground(Color.WHITE);
setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 381);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
textArea.append(textField.getText() + "\n");
textField.setText("");
textField.requestFocus();
}
}
});
textField.setColumns(10);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
);
textArea = new JTextArea();
textArea.setBackground(Color.DARK_GRAY);
textArea.setForeground(Color.WHITE);
textArea.setAlignmentY(Component.BOTTOM_ALIGNMENT);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
contentPane.setLayout(gl_contentPane);
}
}