将多个 Jtextareas 添加到同一个 JScrollPane
Adding multiple Jtextareas into same JScrollPane
我知道有很多关于它的问题,但我都阅读了所有问题。大多数人更喜欢将文本区域添加到面板中并将此面板添加到 JScrollPane 中。我试过了,但没用。
这是我的代码。
AppSed Class
package gui;
import javax.swing.SwingUtilities;
public class AppSed {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new MainFrame();
}
});
}
}
主机Class
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainFrame extends JFrame {
private TextPanel textPanel;
private ToolBar toolBar;
public MainFrame(){
super("Sed Debugger");
setLayout(new BorderLayout());
textPanel = new TextPanel();
toolBar = new ToolBar();
add(textPanel,BorderLayout.CENTER );
add(toolBar, BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(1200,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JMenuBar createMenuBar(){
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu debug = new JMenu("Debug");
JMenu help = new JMenu("Help");
JMenuItem newFile = new JMenuItem("New");
JMenuItem saveFile = new JMenuItem("Open");
JMenuItem openFile = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
file.add(newFile);
file.add(openFile);
file.add(saveFile);
file.add(exit);
JMenuItem run = new JMenuItem("Run");
JMenuItem stop = new JMenuItem("Stop");
JMenuItem refresh = new JMenuItem("Refresh");
JMenuItem stepIn = new JMenuItem("Step In");
JMenuItem stepOut = new JMenuItem("Step Out");
JMenuItem stepOver = new JMenuItem("Step Over");
JMenuItem breakPoint = new JMenuItem("New Breakpoint");
debug.add(run);
debug.add(stop);
debug.add(refresh);
debug.add(stepIn);
debug.add(stepOut);
debug.add(stepOver);
debug.add(breakPoint);
JMenuItem commentHelp = new JMenuItem("Comment Help");
JMenuItem about = new JMenuItem("About");
help.add(commentHelp);
help.add(about);
menuBar.add(file);
menuBar.add(debug);
menuBar.add(help);
return menuBar;
}
}
文本面板class
package gui;
import java.awt.Dimension;
public class TextPanel extends JPanel {
private InputPane input ;
private JScrollPane scrollPane;
public TextPanel(){
setLayout(new GridBagLayout());
input = new InputPane();
scrollPane = new JScrollPane(input);
Dimension dim = scrollPane.getPreferredSize();
dim.width = 510;
dim.height = 290;
scrollPane.setPreferredSize(dim);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 0.25;
add(scrollPane,gc);
}
}
输入面板class
package gui;
import java.awt.BorderLayout...;
public class InputPane extends JPanel {
private JTextArea inputText;
private JTextArea lineNumber;
private JScrollPane scrollPane;
public InputPane(){
setLayout(new BorderLayout());
Dimension dim = getPreferredSize();
dim.width = 500;
dim.height = 250;
setPreferredSize(dim);
setBorder(BorderFactory.createTitledBorder("Original File"));
inputText = new JTextArea();
lineNumber = new JTextArea();
Dimension dimInput = getPreferredSize();
dimInput.width = 440;
dimInput.height = 250;
inputText.setPreferredSize(dimInput);
Dimension lineDim = getPreferredSize();
lineDim.width = 50;
lineDim.height = 250;
lineNumber.setPreferredSize(lineDim);
lineNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.gray));
add(inputText,BorderLayout.EAST);
add(lineNumber,BorderLayout.WEST);
}
inputText = new JTextArea();
lineNumber = new JTextArea();
我认为您应该将行号 textarea 添加到滚动窗格的行 header,而不是在面板上使用两个文本区域。另外,不要使用 setPreferredSize()。它默认使用滚动窗格的目的,因为只有当组件的首选大小大于滚动窗格的大小时,滚动条才会出现。
您可能想要查看 Text Component Line Number,而不是使用两个文本区域,它旨在在文本组件旁边显示行号。行号组件添加到滚动窗格的行 header。
您 "seem" 想创建一个 "split" 行号在左边,文本在右边?
第一步,停止与组件的preferredSize
拧紧。您无法控制这些组件需要用来计算其需求的因素。
有关详细信息,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
相反,使用可用的属性提供提示,这可以让他们计算出他们需要space的数量
查看 JTextArea(int, int)
了解更多详情
例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TextPanel extends JPanel {
private InputPane input;
private JScrollPane scrollPane;
public TextPanel() {
setLayout(new GridBagLayout());
input = new InputPane();
scrollPane = new JScrollPane(input);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 0.25;
add(scrollPane, gc);
}
}
public class InputPane extends JPanel {
private JTextArea inputText;
private JTextArea lineNumber;
public InputPane() {
setLayout(new BorderLayout());
setBorder(BorderFactory.createTitledBorder("Original File"));
inputText = new JTextArea(10, 20);
lineNumber = new JTextArea(10, 4);
lineNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.gray));
add(inputText);
add(lineNumber, BorderLayout.WEST);
}
}
}
现在,话虽如此,我不鼓励您使用此过程,并建议改用 JScrollPane#setRowHeaderView
。有关详细信息,请参阅 this example and How to Use Scroll Panes
我知道有很多关于它的问题,但我都阅读了所有问题。大多数人更喜欢将文本区域添加到面板中并将此面板添加到 JScrollPane 中。我试过了,但没用。
这是我的代码。
AppSed Class
package gui;
import javax.swing.SwingUtilities;
public class AppSed {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new MainFrame();
}
});
}
}
主机Class
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainFrame extends JFrame {
private TextPanel textPanel;
private ToolBar toolBar;
public MainFrame(){
super("Sed Debugger");
setLayout(new BorderLayout());
textPanel = new TextPanel();
toolBar = new ToolBar();
add(textPanel,BorderLayout.CENTER );
add(toolBar, BorderLayout.NORTH);
setJMenuBar(createMenuBar());
setSize(1200,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JMenuBar createMenuBar(){
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu debug = new JMenu("Debug");
JMenu help = new JMenu("Help");
JMenuItem newFile = new JMenuItem("New");
JMenuItem saveFile = new JMenuItem("Open");
JMenuItem openFile = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
file.add(newFile);
file.add(openFile);
file.add(saveFile);
file.add(exit);
JMenuItem run = new JMenuItem("Run");
JMenuItem stop = new JMenuItem("Stop");
JMenuItem refresh = new JMenuItem("Refresh");
JMenuItem stepIn = new JMenuItem("Step In");
JMenuItem stepOut = new JMenuItem("Step Out");
JMenuItem stepOver = new JMenuItem("Step Over");
JMenuItem breakPoint = new JMenuItem("New Breakpoint");
debug.add(run);
debug.add(stop);
debug.add(refresh);
debug.add(stepIn);
debug.add(stepOut);
debug.add(stepOver);
debug.add(breakPoint);
JMenuItem commentHelp = new JMenuItem("Comment Help");
JMenuItem about = new JMenuItem("About");
help.add(commentHelp);
help.add(about);
menuBar.add(file);
menuBar.add(debug);
menuBar.add(help);
return menuBar;
}
}
文本面板class
package gui;
import java.awt.Dimension;
public class TextPanel extends JPanel {
private InputPane input ;
private JScrollPane scrollPane;
public TextPanel(){
setLayout(new GridBagLayout());
input = new InputPane();
scrollPane = new JScrollPane(input);
Dimension dim = scrollPane.getPreferredSize();
dim.width = 510;
dim.height = 290;
scrollPane.setPreferredSize(dim);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 0.25;
add(scrollPane,gc);
}
}
输入面板class
package gui;
import java.awt.BorderLayout...;
public class InputPane extends JPanel {
private JTextArea inputText;
private JTextArea lineNumber;
private JScrollPane scrollPane;
public InputPane(){
setLayout(new BorderLayout());
Dimension dim = getPreferredSize();
dim.width = 500;
dim.height = 250;
setPreferredSize(dim);
setBorder(BorderFactory.createTitledBorder("Original File"));
inputText = new JTextArea();
lineNumber = new JTextArea();
Dimension dimInput = getPreferredSize();
dimInput.width = 440;
dimInput.height = 250;
inputText.setPreferredSize(dimInput);
Dimension lineDim = getPreferredSize();
lineDim.width = 50;
lineDim.height = 250;
lineNumber.setPreferredSize(lineDim);
lineNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.gray));
add(inputText,BorderLayout.EAST);
add(lineNumber,BorderLayout.WEST);
}
inputText = new JTextArea();
lineNumber = new JTextArea();
我认为您应该将行号 textarea 添加到滚动窗格的行 header,而不是在面板上使用两个文本区域。另外,不要使用 setPreferredSize()。它默认使用滚动窗格的目的,因为只有当组件的首选大小大于滚动窗格的大小时,滚动条才会出现。
您可能想要查看 Text Component Line Number,而不是使用两个文本区域,它旨在在文本组件旁边显示行号。行号组件添加到滚动窗格的行 header。
您 "seem" 想创建一个 "split" 行号在左边,文本在右边?
第一步,停止与组件的preferredSize
拧紧。您无法控制这些组件需要用来计算其需求的因素。
有关详细信息,请参阅 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
相反,使用可用的属性提供提示,这可以让他们计算出他们需要space的数量
查看 JTextArea(int, int)
了解更多详情
例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TextPanel extends JPanel {
private InputPane input;
private JScrollPane scrollPane;
public TextPanel() {
setLayout(new GridBagLayout());
input = new InputPane();
scrollPane = new JScrollPane(input);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 0.25;
add(scrollPane, gc);
}
}
public class InputPane extends JPanel {
private JTextArea inputText;
private JTextArea lineNumber;
public InputPane() {
setLayout(new BorderLayout());
setBorder(BorderFactory.createTitledBorder("Original File"));
inputText = new JTextArea(10, 20);
lineNumber = new JTextArea(10, 4);
lineNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 2, Color.gray));
add(inputText);
add(lineNumber, BorderLayout.WEST);
}
}
}
现在,话虽如此,我不鼓励您使用此过程,并建议改用 JScrollPane#setRowHeaderView
。有关详细信息,请参阅 this example and How to Use Scroll Panes