这是我的GUI代码,我添加了ScrollBar,但是它不起作用。 我想添加垂直滚动条和水平滚动条。请任何人帮助我。我尝试了很多,但仍然没有运气
谢谢。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.*;
import java.io.IOException;
import java.util.Scanner;
public class TimeTableGUI extends JFrame implements ActionListener {
private JTextField name = new JTextField(10);
private JTextField day = new JTextField(10);
private JTextField startTime = new JTextField(10);
private JTextField endTime = new JTextField(10);
private JTextField weekPattern = new JTextField(10);
private JTextField location = new JTextField(10);
private JTextField roomSize = new JTextField(10);
private JTextField classSize = new JTextField(10);
private JTextField staff = new JTextField(10);
private JTextField department = new JTextField(10);
private JTextField type = new JTextField(10);
private JLabel labn = new JLabel(" Module Name:");
private JLabel labd = new JLabel(" Day:");
private JLabel labsT = new JLabel(" StartTime:");
private JLabel labeT = new JLabel(" EndTime:");
private JLabel labwP = new JLabel(" WeekPattern:");
private JLabel labl = new JLabel(" Location:");
private JLabel labrS = new JLabel(" RoomSize:");
private JLabel labcS = new JLabel("ClassSize");
private JLabel labs = new JLabel("Staff");
private JLabel labdep = new JLabel("Department");
private JLabel labtyp = new JLabel("Type");
private JButton lecture = new JButton("Lecture");
private JButton computerLab = new JButton("ComputerLab");
private JButton seminar = new JButton("Seminar");
private JButton lookup = new JButton("LookUp");
private JButton add = new JButton("Add");
private JButton remove = new JButton("Remove");
private JButton update = new JButton("Save");
private TimeTable timeTable = new TimeTable();
private boolean seminarSelected = false;
private boolean lectureSelected = false;
private boolean computerLabSelected = false;
private JTextArea outputArea = new JTextArea(20, 50);
我在这里做这件事,
私有JScrollPane滚动条=新的JScrollPane(outputArea);
public static void main(String[] args) throws IOException {
TimeTableGUI applic = new TimeTableGUI();
} // main
public TimeTableGUI() throws IOException {
super("TimeTable");
FlowLayout flowLayout1 = new FlowLayout();
setLayout(flowLayout1);
add(labn);
add(name);
name.setEditable(true);
add(labd);
add(day);
day.setEditable(true);
add(labsT);
add(startTime);
startTime.setEditable(true);
add(labeT);
add(endTime);
endTime.setEditable(true);
add(labwP);
add(weekPattern);
weekPattern.setEditable(true);
add(labl);
add(location);
location.setEditable(true);
add(labrS);
add(roomSize);
roomSize.setEditable(true);
add(labcS);
add(classSize);
classSize.setEditable(true);
add(labs);
add(staff);
staff.setVisible(true);
add(labdep);
add(department);
department.setEditable(true);
add(labtyp);
add(type);
type.setEditable(true);
add(lecture);
lecture.addActionListener(this);
add(computerLab);
computerLab.addActionListener(this);
add(seminar);
seminar.addActionListener(this);
add(lookup);
lookup.addActionListener(this);
add(add);
add.addActionListener(this);
add(remove);
remove.addActionListener(this);
add(update);
update.addActionListener(this);
并且我在这里添加了滚动条,即使它不起作用 添加(滚动条);
outputArea.setEditable(false);
add(outputArea);
setSize(720, 200);
outputArea.setEditable(false);
setVisible(true);
blankDisplay();
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == add) {
String n = name.getText();
String d = day.getText();
String sT = startTime.getText();
String eT = endTime.getText();
String wP = weekPattern.getText();
String l = location.getText();
String rS = roomSize.getText();
String cS = classSize.getText();
String s = staff.getText();
String dep = department.getText();
String ty = type.getText();
outputArea.setText("Record added\n");
if (seminarSelected) {
ty = type.getText();
Seminar S = new Seminar(n, d, sT, eT, wP, l, rS, cS, s, dep);
timeTable.addEntry(S);
} else if (lectureSelected) {
ty = type.getText();
Lecture L = new Lecture(n, d, sT, eT, wP, l, rS, cS, s, dep);
timeTable.addEntry(L);
} else if (computerLabSelected) {
ty = type.getText();
ComputerLab cL = new ComputerLab(n, d, sT, eT, wP, l, rS, cS, s, dep);
timeTable.addEntry(cL);
} else {
Entry entry = new Entry(n, d, sT, eT, wP, l, rS, cS, s, dep);
timeTable.addEntry(entry);
}
}
if (event.getSource() == lookup) {
String n = name.getText();
String result = timeTable.lookupEntry(n);
outputArea.setText(result);
}
if (event.getSource() == remove) {
String n = name.getText();
String result = timeTable.removeEntry(n);
//timeTable.removeEntry(name.getText());
outputArea.setText(result);
outputArea.setText("Record removed");
}
if (event.getSource() == update) {
// timeTable.updateEntry();
outputArea.setText("Record Updated");
}
}
public void blankDisplay() {
name.setText("");
day.setText("");
startTime.setText("");
startTime.setText("");
endTime.setText("");
weekPattern.setText("");
location.setText("");
roomSize.setText("");
classSize.setText("");
staff.setText("");
department.setText("");
}
}
答案 0 :(得分:0)
我正在这里添加滚动条,即使它不起作用add(scrollbar);
在哪里?我看不到将滚动窗格添加到框架的位置。
我所看到的是:
2014-12-06 NaN
2014-12-13 NaN
2014-12-20 NaN
2014-12-27 NaN
2015-01-03 NaN
...
这是错误的,因为一个组件只能有一个父对象,所以这将从滚动窗格中删除文本区域。
代码应为:
add(outputArea);