我知道有很多问题,但我读了所有这些问题。大多数人更喜欢将textareas添加到面板中并将此面板添加到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();
}
});
}
}
MainFrame类
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;
}
}
TextPanel类
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);
}
}
InputPane类
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);
}
答案 0 :(得分:1)
inputText = new JTextArea();
lineNumber = new JTextArea();
我没有在面板上使用两个文本区域,而是认为您应该将行号textarea添加到滚动窗格的行标题中。另外,不要使用setPreferredSize()。它默认使用滚动窗格的目的,因为只有当组件的首选大小大于滚动窗格的大小时,滚动条才会出现。
您可能需要查看Text Component Line Number,而不是使用两个文本区域,该{{3}}用于在文本组件旁边显示行号。行号组件将添加到滚动窗格的行标题中。
答案 1 :(得分:1)
你"似乎"试图创造一个"分裂"左边的行号和右边的文字?
第一步,停止使用preferredSize
组件拧紧。您无法控制这些组件计算其需求所需的因素。
有关详细信息,请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
相反,使用可用的属性来提供提示,这可以让他们计算出他们需要的空间量
请查看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和How to Use Scroll Panes