Hi,
ich versuche der Textarea textAreaQuestion eine Scrollbar hinzuzufügen. Der Code funktioniert einwandfrei wenn ich die Scrollbar weglasse. Wenn ich sie allerdings versuche wie unten im COde anzufügen verschwindet die gesamte Textare und wird nicht mehr angezeigt. Vielleicht kann mir jemand helfen bzw. einen Denkanstoss geben ?!?
DAnke,
ich versuche der Textarea textAreaQuestion eine Scrollbar hinzuzufügen. Der Code funktioniert einwandfrei wenn ich die Scrollbar weglasse. Wenn ich sie allerdings versuche wie unten im COde anzufügen verschwindet die gesamte Textare und wird nicht mehr angezeigt. Vielleicht kann mir jemand helfen bzw. einen Denkanstoss geben ?!?
Java:
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class Abfrage extends JFrame {
private JPanel contentPane;
private JTextField txtNumber;
private int aktuelleFrage = 1;
private String antwort;
Color selected = new Color(212, 212, 212);
Color deselected = new Color(238, 238, 238);
Color richtig = new Color(0, 255, 0);
Color falsch = new Color(255, 0, 0);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Abfrage frame = new Abfrage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Abfrage() {
}
public Abfrage(String sql, String verwendung, String muster) {
super();
MySQL verbindung = new MySQL();
Question[] questionset = verbindung.database_read(sql);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 800, 700);
setResizable(false);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBackground(UIManager.getColor("Separator.foreground"));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.setBounds(6, 6, 560, 220);
panel.setLayout(null);
contentPane.add(panel);
JTextArea textAreaQuestion = new JTextArea();
textAreaQuestion.setEditable(false);
textAreaQuestion.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
textAreaQuestion.setLineWrap(true);
textAreaQuestion.setWrapStyleWord(true);
textAreaQuestion.setBounds(6, 84, 548, 130);
textAreaQuestion.setText(questionset[aktuelleFrage - 1].getQuestion());
panel.add(new JScrollPane(textAreaQuestion));
JLabel lblNewLabel = new JLabel("Frage");
lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
lblNewLabel.setBounds(6, 6, 62, 26);
panel.add(lblNewLabel);
txtNumber = new JTextField();
txtNumber.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
txtNumber.setBounds(64, 8, 62, 27);
txtNumber.setText(Integer.toString(aktuelleFrage));
panel.add(txtNumber);
txtNumber.setColumns(10);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel_1.setBackground(selected);
panel_1.setBounds(6, 238, 788, 434);
contentPane.add(panel_1);
panel_1.setLayout(null);
JTextArea answer_A = new JTextArea();
answer_A.setBackground(UIManager.getColor("Panel.background"));
answer_A.setEditable(false);
answer_A.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
answer_A.setLineWrap(true);
answer_A.setWrapStyleWord(true);
answer_A.setBounds(80, 10, 476, 77);
answer_A.setText(questionset[aktuelleFrage - 1].getAnswerA());
answer_A.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
panel_1.add(answer_A);
// Antwortfeld 2
JTextArea answer_B = new JTextArea();
answer_B.setBackground(UIManager.getColor("Panel.background"));
answer_B.setEditable(false);
answer_B.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
answer_B.setLineWrap(true);
answer_B.setWrapStyleWord(true);
answer_B.setBounds(80, 94, 476, 77);
answer_B.setText(questionset[aktuelleFrage - 1].getAnswerB());
answer_B.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
panel_1.add(answer_B);
// Antwortfeld 3
JTextArea answer_C = new JTextArea();
answer_C.setBackground(UIManager.getColor("Panel.background"));
answer_C.setEditable(false);
answer_C.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
answer_C.setLineWrap(true);
answer_C.setWrapStyleWord(true);
answer_C.setBounds(80, 178, 476, 77);
answer_C.setText(questionset[aktuelleFrage - 1].getAnswerC());
answer_C.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
panel_1.add(answer_C);
// Antwortfeld 4
JTextArea answer_D = new JTextArea();
answer_D.setBackground(UIManager.getColor("Panel.background"));
answer_D.setEditable(false);
answer_D.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
answer_D.setLineWrap(true);
answer_D.setWrapStyleWord(true);
answer_D.setBounds(80, 262, 476, 77);
answer_D.setText(questionset[aktuelleFrage - 1].getAnswerD());
answer_D.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
panel_1.add(answer_D);
// Antwortfeld 5
JTextArea answer_E = new JTextArea();
answer_E.setBackground(UIManager.getColor("Panel.background"));
answer_E.setEditable(false);
answer_E.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
answer_E.setLineWrap(true);
answer_E.setWrapStyleWord(true);
answer_E.setBounds(80, 346, 476, 77);
answer_E.setText(questionset[aktuelleFrage - 1].getAnswerE());
answer_E.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
panel_1.add(answer_E);
JLabel lblMusterFrage = new JLabel("");
lblMusterFrage.setForeground(Color.RED);
lblMusterFrage.setFont(new Font("Lucida Grande", Font.BOLD, 18));
lblMusterFrage.setBounds(355, 37, 199, 35);
if(questionset[aktuelleFrage - 1].gete90() != 0){
lblMusterFrage.setText("!!! EMJ 190 !!!");
}else if (questionset[aktuelleFrage - 1].getE95() != 0){
lblMusterFrage.setText("!!! EMJ 195 !!!");
}else if (questionset[aktuelleFrage - 1].getCr9() != 0){
lblMusterFrage.setText("!!! CRJ 900 !!!");
}else if (questionset[aktuelleFrage - 1].getC9x() != 0){
lblMusterFrage.setText("!!! CRJ Next GEN !!!");
}else{
lblMusterFrage.setText("");
}
panel.add(lblMusterFrage);
// Beschriftung der Antwortmöglichkeiten
JButton buttonA = new JButton("A.)");
buttonA.setFont(new Font("Lucida Grande", Font.BOLD, 18));
buttonA.setBounds(16, 10, 52, 77);
panel_1.add(buttonA);
JButton buttonB = new JButton("B.)");
buttonB.setActionCommand("b");
buttonB.setFont(new Font("Lucida Grande", Font.BOLD, 18));
buttonB.setBounds(16, 94, 52, 77);
panel_1.add(buttonB);
JButton buttonC = new JButton("C.)");
buttonC.setActionCommand("c");
buttonC.setFont(new Font("Lucida Grande", Font.BOLD, 18));
buttonC.setBounds(16, 183, 52, 77);
panel_1.add(buttonC);
JButton buttonD = new JButton("D.)");
buttonD.setFont(new Font("Lucida Grande", Font.BOLD, 18));
buttonD.setBounds(16, 262, 52, 77);
panel_1.add(buttonD);
JButton buttonE = new JButton("E.)");
buttonE.setFont(new Font("Lucida Grande", Font.BOLD, 18));
buttonE.setBounds(16, 346, 52, 77);
panel_1.add(buttonE);
buttonA.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
antwort = "a";
if (antwort.equals(questionset[aktuelleFrage - 1].getCheck())) {
naechsteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
} else {
answer_A.setBackground(falsch);
}
}
});
buttonB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
antwort = "b";
if (antwort.equals(questionset[aktuelleFrage - 1].getCheck())) {
naechsteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
} else {
answer_B.setBackground(falsch);
}
}
});
buttonC.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
antwort = "c";
if (antwort.equals(questionset[aktuelleFrage - 1].getCheck())) {
naechsteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
} else {
answer_C.setBackground(falsch);
}
}
});
buttonD.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
antwort = "d";
if (antwort.equals(questionset[aktuelleFrage - 1].getCheck())) {
naechsteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
} else {
answer_D.setBackground(falsch);
}
}
});
buttonE.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
antwort = "e";
if (antwort.equals(questionset[aktuelleFrage - 1].getCheck())) {
naechsteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
} else {
answer_E.setBackground(falsch);
}
}
});
JButton btnGoto = new JButton("gehe zu");
btnGoto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gehezu(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
}
});
btnGoto.setBounds(138, 7, 85, 30);
panel.add(btnGoto);
JButton btnZurueck = new JButton("zurueck");
btnZurueck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
letzteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
}
});
btnZurueck.setBounds(6, 47, 85, 30);
panel.add(btnZurueck);
JButton btnVor = new JButton("vor");
btnVor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
naechsteFrage(questionset, textAreaQuestion, answer_A, answer_B, answer_C, answer_D, answer_E, lblMusterFrage);
}
});
btnVor.setBounds(103, 47, 85, 30);
panel.add(btnVor);
JLabel lblUser = new JLabel(AktuellerUser.dlc + " / " + AktuellerUser.nachname);
lblUser.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
lblUser.setBounds(578, 6, 199, 35);
contentPane.add(lblUser);
JLabel lblVerwendung = new JLabel(verwendung);
lblVerwendung.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
lblVerwendung.setBounds(677, 56, 100, 35);
contentPane.add(lblVerwendung);
JLabel lblMuster = new JLabel(muster);
lblMuster.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
lblMuster.setBounds(677, 106, 100, 35);
contentPane.add(lblMuster);
JLabel lblVerwendung_1 = new JLabel("Daten:");
lblVerwendung_1.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
lblVerwendung_1.setBounds(578, 56, 80, 35);
contentPane.add(lblVerwendung_1);
JLabel lblMuster_1 = new JLabel("Muster:");
lblMuster_1.setFont(new Font("Lucida Grande", Font.PLAIN, 14));
lblMuster_1.setBounds(578, 106, 80, 35);
contentPane.add(lblMuster_1);
setVisible(true);
}
private void deselectAll(JTextArea answer_A, JTextArea answer_B, JTextArea answer_C, JTextArea answer_D,
JTextArea answer_E) {
answer_A.setBackground(deselected);
answer_B.setBackground(deselected);
answer_C.setBackground(deselected);
answer_D.setBackground(deselected);
answer_E.setBackground(deselected);
}
private void naechsteFrage(Question[] questionset, JTextArea textAreaQuestion, JTextArea answer_A,
JTextArea answer_B, JTextArea answer_C, JTextArea answer_D, JTextArea answer_E, JLabel lblMusterFrage) {
if (aktuelleFrage < Question.lastQ) {
txtNumber.setText(Integer.toString(++aktuelleFrage));
textAreaQuestion.setText(questionset[aktuelleFrage - 1].getQuestion());
answer_A.setText(questionset[aktuelleFrage - 1].getAnswerA());
answer_B.setText(questionset[aktuelleFrage - 1].getAnswerB());
answer_C.setText(questionset[aktuelleFrage - 1].getAnswerC());
answer_D.setText(questionset[aktuelleFrage - 1].getAnswerD());
answer_E.setText(questionset[aktuelleFrage - 1].getAnswerE());
deselectAll(answer_A, answer_B, answer_C, answer_D, answer_E);
if(questionset[aktuelleFrage - 1].gete90() != 0){
lblMusterFrage.setText("!!! EMJ 190 !!!");
}else if (questionset[aktuelleFrage - 1].getE95() != 0){
lblMusterFrage.setText("!!! EMJ 195 !!!");
}else if (questionset[aktuelleFrage - 1].getCr9() != 0){
lblMusterFrage.setText("!!! CRJ 900 !!!");
}else if (questionset[aktuelleFrage - 1].getC9x() != 0){
lblMusterFrage.setText("!!! CRJ Next GEN !!!");
}else{
lblMusterFrage.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "Letzte Frage");
}
}
private void letzteFrage(Question[] questionset, JTextArea textAreaQuestion, JTextArea answer_A, JTextArea answer_B,
JTextArea answer_C, JTextArea answer_D, JTextArea answer_E, JLabel lblMusterFrage) {
if (aktuelleFrage > 1) {
txtNumber.setText(Integer.toString(--aktuelleFrage));
textAreaQuestion.setText(questionset[aktuelleFrage - 1].getQuestion());
answer_A.setText(questionset[aktuelleFrage - 1].getAnswerA());
answer_B.setText(questionset[aktuelleFrage - 1].getAnswerB());
answer_C.setText(questionset[aktuelleFrage - 1].getAnswerC());
answer_D.setText(questionset[aktuelleFrage - 1].getAnswerD());
answer_E.setText(questionset[aktuelleFrage - 1].getAnswerE());
deselectAll(answer_A, answer_B, answer_C, answer_D, answer_E);
if(questionset[aktuelleFrage - 1].gete90() != 0){
lblMusterFrage.setText("!!! EMJ 190 !!!");
}else if (questionset[aktuelleFrage - 1].getE95() != 0){
lblMusterFrage.setText("!!! EMJ 195 !!!");
}else if (questionset[aktuelleFrage - 1].getCr9() != 0){
lblMusterFrage.setText("!!! CRJ 900 !!!");
}else if (questionset[aktuelleFrage - 1].getC9x() != 0){
lblMusterFrage.setText("!!! CRJ Next GEN !!!");
}else{
lblMusterFrage.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "Erste Frage erreicht");
}
}
private void gehezu(Question[] questionset, JTextArea textAreaQuestion, JTextArea answer_A, JTextArea answer_B,
JTextArea answer_C, JTextArea answer_D, JTextArea answer_E, JLabel lblMusterFrage) {
if (txtNumber.getText().matches("\\d*")) {
if (Integer.parseInt(txtNumber.getText()) >= 1 && Integer.parseInt(txtNumber.getText()) <= Question.lastQ) {
aktuelleFrage = Integer.parseInt(txtNumber.getText());
textAreaQuestion.setText(questionset[aktuelleFrage - 1].getQuestion());
answer_A.setText(questionset[aktuelleFrage - 1].getAnswerA());
answer_B.setText(questionset[aktuelleFrage - 1].getAnswerB());
answer_C.setText(questionset[aktuelleFrage - 1].getAnswerC());
answer_D.setText(questionset[aktuelleFrage - 1].getAnswerD());
answer_E.setText(questionset[aktuelleFrage - 1].getAnswerE());
deselectAll(answer_A, answer_B, answer_C, answer_D, answer_E);
if(questionset[aktuelleFrage - 1].gete90() != 0){
lblMusterFrage.setText("!!! EMJ 190 !!!");
}else if (questionset[aktuelleFrage - 1].getE95() != 0){
lblMusterFrage.setText("!!! EMJ 195 !!!");
}else if (questionset[aktuelleFrage - 1].getCr9() != 0){
lblMusterFrage.setText("!!! CRJ 900 !!!");
}else if (questionset[aktuelleFrage - 1].getC9x() != 0){
lblMusterFrage.setText("!!! CRJ Next GEN !!!");
}else{
lblMusterFrage.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "Ungueltige Eingabe");
}
} else {
JOptionPane.showMessageDialog(null, "Ungueltige Eingabe");
}
}
}
DAnke,