Java 将矢量打印到JTextArea时的间距错误
所以我正在制作一个建立在JavaSwing GUI之上的数据库系统。。。我有一个按钮可以将个人/事物添加到向量(在本例中为数据库): 这部分似乎都很好,但是,当我使用字符串缓冲区在JTextArea上打印向量时,JTextArea上的文本中存在奇怪的间距问题 下面是StringBuffer和我将向量打印到JTextArea的部分:Java 将矢量打印到JTextArea时的间距错误,java,database,swing,vector,stringbuffer,Java,Database,Swing,Vector,Stringbuffer,所以我正在制作一个建立在JavaSwing GUI之上的数据库系统。。。我有一个按钮可以将个人/事物添加到向量(在本例中为数据库): 这部分似乎都很好,但是,当我使用字符串缓冲区在JTextArea上打印向量时,JTextArea上的文本中存在奇怪的间距问题 下面是StringBuffer和我将向量打印到JTextArea的部分: StringBuffer dbb = new StringBuffer(); for (int i = 0; i < db.size(); i++) {
StringBuffer dbb = new StringBuffer();
for (int i = 0; i < db.size(); i++) {
dbb.append(db.get(i) + '\n');
}
// printDB is the JTextArea
printDB.setText(dbb.toString());
add(printDB);
StringBuffer dbb=new-StringBuffer();
对于(int i=0;i
间距问题的屏幕截图:
你知道是什么引起的吗?间距似乎也是线性的(1空间、2空间、3空间…)
- 链接到完整的项目,如果需要的话(对不起,一般来说,糟糕的代码lol,我才刚刚开始):
- 对不起,如果线性不是正确的词,顺便说一句,我想不出另一种方式来描述它
import java.awt.*;
导入java.awt.event.*;
导入java.io.IOException;
导入java.util.*;
导入java.util.Vector.*;
导入javax.swing.*;
公共类数据库扩展JFrame实现ActionListener、EventListener{
//数据库
向量db=新向量();
//主菜单按钮:
JButton addStudent=新JButton(“添加学生”);
JButton deletestustudent=新JButton(“删除按钮”);
JButton deleteAll=新JButton(“删除所有学生”);
JButton printAll=新JButton(“打印数据库”);
JTextArea welcome=新的JTextArea(“welcome!”);
//添加学员菜单:
JTextField student=新JTextField();
JButton submit=新JButton(“添加学生”);
//印刷学生
JTextArea printDB=新的JTextArea();
JButton returnMenu=新JButton(“返回菜单”);
公共数据库(){
超级(“数据库GUI”);
设置大小(800600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(空);
可设置大小(假);
welcome.setBackground(this.get前台());
添加(欢迎);
欢迎。设置大小(60,15);
欢迎。设置位置(386300);
添加(添加学生);
addStudent.setSize(150,50);
addStudent.setLocation(25100);
添加(删除学生);
deleteStudent.setSize(150,50);
删除学生设置位置(625100);
添加(全部删除);
删除所有设置位置(225100);
deleteAll.setSize(150,50);
添加(打印全部);
printAll.setLocation(425100);
printAll.setSize(150,50);
addStudent.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
welcome.setVisible(false);
addStudent.setVisible(false);
deleteStudent.setVisible(false);
deleteAll.setVisible(false);
printAll.setVisible(假);
加(学生);
添加(提交);
submit.setVisible(true);
提交.设置大小(150,30);
提交。设置位置(425250);
student.setVisible(true);
学生。设置大小(150,30);
学生。设置位置(275250);
submit.addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
字符串newStudent=student.getText();
db.附录(新学生);
student.setText(null);
student.setVisible(false);
submit.setVisible(false);
欢迎。setVisible(true);
addStudent.setVisible(true);
deleteStudent.setVisible(true);
deleteAll.setVisible(true);
printAll.setVisible(true);
}
});
}
});
printAll.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
welcome.setVisible(false);
addStudent.setVisible(false);
deleteStudent.setVisible(false);
deleteAll.setVisible(false);
printAll.setVisible(假);
StringBuffer dbb=新的StringBuffer();
对于(int i=0;i
您正在addStudent ActionListener中重复向submit按钮添加ActionListener,这意味着当按下addStudent时,将有越来越多的ActionListener添加到submit,而这不是您想要的
建议:
- 只需将ActionListener添加到JButtons中一次,而不添加到其他可能被多次调用的事件侦听器中。考虑在类构造函数中添加所有ActuiListInter。
- 不要使用绝对定位和空布局。而空布局
new ActionListener() { public void actionPerformed(ActionEvent e) { String newStudent = student.getText(); db.addElement(newStudent);
StringBuffer dbb = new StringBuffer(); for (int i = 0; i < db.size(); i++) { dbb.append(db.get(i) + '\n'); } // printDB is the JTextArea printDB.setText(dbb.toString()); add(printDB);
import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.util.*; import java.util.Vector.*; import javax.swing.*; public class Database extends JFrame implements ActionListener, EventListener { // Database Vector<String> db = new Vector<String>(); // Main Menu Buttons: JButton addStudent = new JButton("Add Student"); JButton deleteStudent = new JButton("Delete Button"); JButton deleteAll = new JButton("Delete All Students"); JButton printAll = new JButton("Print Database"); JTextArea welcome = new JTextArea("Welcome!"); // Add Student Menu: JTextField student = new JTextField(); JButton submit = new JButton("Add Student"); // Print Students JTextArea printDB = new JTextArea(); JButton returnMenu = new JButton("Return to Menu"); public Database() { super("DatabaseGUI"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); setResizable(false); welcome.setBackground(this.getForeground()); add(welcome); welcome.setSize(60, 15); welcome.setLocation(386, 300); add(addStudent); addStudent.setSize(150, 50); addStudent.setLocation(25, 100); add(deleteStudent); deleteStudent.setSize(150, 50); deleteStudent.setLocation(625, 100); add(deleteAll); deleteAll.setLocation(225, 100); deleteAll.setSize(150, 50); add(printAll); printAll.setLocation(425, 100); printAll.setSize(150, 50); addStudent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { welcome.setVisible(false); addStudent.setVisible(false); deleteStudent.setVisible(false); deleteAll.setVisible(false); printAll.setVisible(false); add(student); add(submit); submit.setVisible(true); submit.setSize(150, 30); submit.setLocation(425, 250); student.setVisible(true); student.setSize(150, 30); student.setLocation(275, 250); submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String newStudent = student.getText(); db.addElement(newStudent); student.setText(null); student.setVisible(false); submit.setVisible(false); welcome.setVisible(true); addStudent.setVisible(true); deleteStudent.setVisible(true); deleteAll.setVisible(true); printAll.setVisible(true); } }); } }); printAll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { welcome.setVisible(false); addStudent.setVisible(false); deleteStudent.setVisible(false); deleteAll.setVisible(false); printAll.setVisible(false); StringBuffer dbb = new StringBuffer(); for (int i = 0; i < db.size(); i++) { dbb.append(db.get(i) + '\n'); } printDB.setText(dbb.toString()); add(printDB); printDB.setSize(300, 400); printDB.setEditable(false); printDB.setLocation(100, 100); printDB.setVisible(true); add(returnMenu); returnMenu.setVisible(true); returnMenu.setSize(200, 30); returnMenu.setLocation(500, 400); returnMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { returnMenu.setVisible(false); printDB.setVisible(false); welcome.setVisible(true); addStudent.setVisible(true); deleteStudent.setVisible(true); deleteAll.setVisible(true); printAll.setVisible(true); } }); } }); setVisible(true); } public static void main(String[] args) { Database student = new Database(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import javax.swing.*; @SuppressWarnings("serial") public class Database2 extends JPanel { // constants for the cards public static final String WELCOME = "welcome"; public static final String ADD_STUDENT = "add student"; public static final String DISPLAY_DATA = "display data"; private JTextArea displayTextArea = new JTextArea(15, 20); private JTextField addStudentField = new JTextField(10); private CardLayout cardLayout = new CardLayout(); private List<String> db = new ArrayList<>(); public Database2() { // prepare JTextArea displayTextArea.setWrapStyleWord(true); displayTextArea.setLineWrap(true); displayTextArea.setFocusable(false); // set layout as CardLayout and add all JPanels with constants setLayout(cardLayout); add(createWelcomePanel(), WELCOME); add(createAddStudentPanel(), ADD_STUDENT); add(createDisplayDataPanel(), DISPLAY_DATA); } private JPanel createWelcomePanel() { ShowStudentPanelAction showStudentAction = new ShowStudentPanelAction("Add Student"); DisplayDataAction displayDataAction = new DisplayDataAction("Display Data"); JButton addStudentButton = new JButton(showStudentAction); JButton displayDataButton = new JButton(displayDataAction); JPanel topPanel = new JPanel(new GridLayout(1, 0, 5, 0)); topPanel.add(addStudentButton); topPanel.add(displayDataButton); topPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X))); JLabel welcomeLabel = new JLabel("Welcome", SwingConstants.CENTER); // make JLabel text bigger welcomeLabel.setFont(welcomeLabel.getFont().deriveFont(Font.BOLD, 42f)); // and give it a border 30 points wide int ebGap = 30; welcomeLabel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap)); JPanel welcomePanel = new JPanel(new BorderLayout()); ebGap = 4; welcomePanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap)); welcomePanel.add(topPanel, BorderLayout.PAGE_START); welcomePanel.add(welcomeLabel, BorderLayout.CENTER); return welcomePanel; } private JPanel createAddStudentPanel() { AddStudentAction addStudentAction = new AddStudentAction("Add Student"); addStudentField.setAction(addStudentAction); JPanel addStudentPanel = new JPanel(); addStudentPanel.add(addStudentField); addStudentPanel.add(new JButton(addStudentAction)); return addStudentPanel; } private JPanel createDisplayDataPanel() { JPanel displayDataPanel = new JPanel(); JScrollPane scrollPane = new JScrollPane(displayTextArea); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); displayDataPanel.add(scrollPane); displayDataPanel.add(new JButton(new ReturnToWelcomeAction("Return"))); return displayDataPanel; } private class ShowStudentPanelAction extends AbstractAction { public ShowStudentPanelAction(String name) { super(name); int mnemonic = (int) name.charAt(0); putValue(MNEMONIC_KEY, mnemonic); } @Override public void actionPerformed(ActionEvent e) { cardLayout.show(Database2.this, ADD_STUDENT); addStudentField.requestFocusInWindow(); addStudentField.selectAll(); } } private class DisplayDataAction extends AbstractAction { public DisplayDataAction(String name) { super(name); int mnemonic = (int) name.charAt(0); putValue(MNEMONIC_KEY, mnemonic); } @Override public void actionPerformed(ActionEvent e) { StringBuilder sb = new StringBuilder(); for (String studentName : db) { sb.append(studentName + "\n"); } displayTextArea.setText(sb.toString()); cardLayout.show(Database2.this, DISPLAY_DATA); } } private class AddStudentAction extends AbstractAction { public AddStudentAction(String name) { super(name); int mnemonic = (int) name.charAt(0); putValue(MNEMONIC_KEY, mnemonic); } @Override public void actionPerformed(ActionEvent e) { String studentText = addStudentField.getText(); db.add(studentText); cardLayout.show(Database2.this, WELCOME); } } private class ReturnToWelcomeAction extends AbstractAction { public ReturnToWelcomeAction(String name) { super(name); int mnemonic = (int) name.charAt(0); putValue(MNEMONIC_KEY, mnemonic); } @Override public void actionPerformed(ActionEvent e) { cardLayout.show(Database2.this, WELCOME); } } private class ExitAction extends AbstractAction { public ExitAction(String name, int mnemonic) { super(name); putValue(MNEMONIC_KEY, mnemonic); } @Override public void actionPerformed(ActionEvent e) { Window window = SwingUtilities.getWindowAncestor(Database2.this); if (window != null) { window.dispose(); } } } private static void createAndShowGui() { Database2 mainPanel = new Database2(); JFrame frame = new JFrame("Database2"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }