Hallo, ich habe folgendes Problem:
Hier werden zwanzig Buttons auf einem JPanel dargestellt, diese dienen im Moment nur als Platzhalter, später werden sie durch JPanels ersetzt, was aber das Problem nicht beeinflusst.
Ich möchte gerne durch die Buttons scrollen können. Also habe ich eine JScrollPane hinzugefügt. Sie zum mit den Bounds abgeglichen die das JPanel hat und zum ContentPane hinzugefügt. Es taucht aber keine ScrollBar auf und ich kriege nicht raus weshalb. Es wäre super wenn einer von euch mir das erklären könnte oder jemand einen Beitrag kennt oder fidnet der mein Problem beschreibt. Ich habe keinen gefunden.
Ich habe meine Problemquellenvermutung mit "// XXX hier vermute ich das Problem " markiert.
Vielen Dank schonmal.
kwiff
Hier werden zwanzig Buttons auf einem JPanel dargestellt, diese dienen im Moment nur als Platzhalter, später werden sie durch JPanels ersetzt, was aber das Problem nicht beeinflusst.
Ich möchte gerne durch die Buttons scrollen können. Also habe ich eine JScrollPane hinzugefügt. Sie zum mit den Bounds abgeglichen die das JPanel hat und zum ContentPane hinzugefügt. Es taucht aber keine ScrollBar auf und ich kriege nicht raus weshalb. Es wäre super wenn einer von euch mir das erklären könnte oder jemand einen Beitrag kennt oder fidnet der mein Problem beschreibt. Ich habe keinen gefunden.
Ich habe meine Problemquellenvermutung mit "// XXX hier vermute ich das Problem " markiert.
Java:
package v1;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;
public class G_Manager extends javax.swing.JFrame {
private javax.swing.JButton b_bold;
private javax.swing.JButton b_delete;
private javax.swing.JButton b_italic;
private javax.swing.JButton b_save;
private javax.swing.JButton b_yellow;
public javax.swing.JEditorPane ep_editor;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar menuBar;
public javax.swing.JPanel p_display;
private javax.swing.JPanel p_editor;
private javax.swing.JPanel p_operators;
private JScrollPane sp_editor;
public JScrollPane scroll;
private Control c;
public G_Manager(Control c) {
this.c = c;
// Set look and feel to GTK
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(info.getClassName())) {
try {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
p_editor = new javax.swing.JPanel();
sp_editor = new JScrollPane();
ep_editor = new javax.swing.JEditorPane();
p_display = new javax.swing.JPanel();
p_operators = new javax.swing.JPanel();
b_yellow = new javax.swing.JButton();
b_save = new javax.swing.JButton();
b_delete = new javax.swing.JButton();
b_italic = new javax.swing.JButton();
b_bold = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
scroll = new JScrollPane(p_display);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// ep_editor.setContentType("text/html");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("IMS");
ImageIcon img = new ImageIcon("/home/luca/eclipse-workspace/IMS/logo_300x300.png");
setIconImage(img.getImage());
p_editor.setBackground(new java.awt.Color(101, 101, 101));
ep_editor.setToolTipText("Type here for creating and searching for Information");
sp_editor.setViewportView(ep_editor);
javax.swing.GroupLayout p_editorLayout = new javax.swing.GroupLayout(p_editor);
p_editor.setLayout(p_editorLayout);
p_editorLayout.setHorizontalGroup(p_editorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p_editorLayout.createSequentialGroup()
.addGap(3, 3, 3).addComponent(sp_editor).addGap(3, 3, 3)));
p_editorLayout.setVerticalGroup(p_editorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
p_editorLayout.createSequentialGroup().addGap(3, 3, 3)
.addComponent(sp_editor, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
.addGap(3, 3, 3)));
p_display.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
p_display.setMinimumSize(new java.awt.Dimension(0, 0));
p_display.setPreferredSize(new java.awt.Dimension(2, 2));
javax.swing.GroupLayout p_displayLayout = new javax.swing.GroupLayout(p_display);
p_display.setLayout(p_displayLayout);
p_displayLayout.setHorizontalGroup(p_displayLayout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 1020, Short.MAX_VALUE));
p_displayLayout.setVerticalGroup(p_displayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 442, Short.MAX_VALUE));
p_operators.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
b_yellow.setFont(new java.awt.Font("Noto Sans", 0, 18)); // NOI18N
b_yellow.setText("<html><span style=\"background-color: #FFFF00\"> Y </span>");
b_yellow.setToolTipText("CTRL + Y");
b_yellow.setPreferredSize(new java.awt.Dimension(40, 40));
b_yellow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_yellowActionPerformed(evt);
}
});
b_save.setText("SAVE");
b_save.setToolTipText("CTRL + S");
b_save.setPreferredSize(new java.awt.Dimension(40, 40));
b_save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_saveActionPerformed(evt);
}
});
b_delete.setText("DELETE");
b_delete.setToolTipText("DEL / ENTF");
b_delete.setPreferredSize(new java.awt.Dimension(40, 40));
b_delete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_deleteActionPerformed(evt);
}
});
b_italic.setFont(new java.awt.Font("Noto Sans", 2, 18)); // NOI18N
b_italic.setText("I");
b_italic.setToolTipText("CTRL + I");
b_italic.setPreferredSize(new java.awt.Dimension(40, 40));
b_italic.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_italicActionPerformed(evt);
}
});
b_bold.setFont(new java.awt.Font("Noto Sans", 1, 18)); // NOI18N
b_bold.setText("B");
b_bold.setToolTipText("CTRL + B");
b_bold.setPreferredSize(new java.awt.Dimension(40, 40));
b_bold.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_boldActionPerformed(evt);
}
});
javax.swing.GroupLayout p_operatorsLayout = new javax.swing.GroupLayout(p_operators);
p_operators.setLayout(p_operatorsLayout);
p_operatorsLayout.setHorizontalGroup(p_operatorsLayout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(p_operatorsLayout.createSequentialGroup().addContainerGap().addGroup(p_operatorsLayout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(p_operatorsLayout.createSequentialGroup()
.addComponent(b_yellow, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b_bold, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addComponent(b_italic, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12))
.addGroup(
p_operatorsLayout.createSequentialGroup()
.addComponent(b_save, javax.swing.GroupLayout.PREFERRED_SIZE, 65,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b_delete, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))));
p_operatorsLayout.setVerticalGroup(p_operatorsLayout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(p_operatorsLayout.createSequentialGroup().addContainerGap()
.addGroup(p_operatorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b_yellow, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b_italic, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b_bold, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(p_operatorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b_save, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b_delete, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap()));
// menus
jMenu1.setText("File");
menuBar.add(jMenu1);
jMenu2.setText("Edit");
menuBar.add(jMenu2);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(p_display, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 1022, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(p_editor, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(0, 0, 0).addComponent(p_operators, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 0, 0)));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, 0)
.addComponent(p_display, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
.addGap(0, 0, 0)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(p_editor, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(p_operators, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 0, 0)));
ep_editor.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode() + " pressed");
// save CTRL+S or F5
if ((e.getKeyCode() == 116)
|| ((e.getKeyCode() == 83) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0))) {
c.save();
String text[] = ep_editor.getText().split(" ");
ep_editor.setText(text[0]);
}
// BIG CTRL+B or F2
if ((e.getKeyCode() == 113)
|| ((e.getKeyCode() == 66) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0))) {
System.out.println("BIG"); // TODO
}
// KURSIV CTRL+I or F3
if ((e.getKeyCode() == 114)
|| ((e.getKeyCode() == 73) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0))) {
System.out.println("KURSIV"); // TODO
}
// YELLOW CTRL+Y or CTRL+H or F4
if ((e.getKeyCode() == 115) || ((e.getKeyCode() == 89)
|| (e.getKeyCode() == 72) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0))) {
System.out.println("YELLOW"); // TODO
}
// Clear F7 or CTRL+D
if ((e.getKeyCode() == 118)
|| ((e.getKeyCode() == 68) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0))) {
ep_editor.setText("");
}
// DELETE ENTF
if (e.getKeyCode() == 127) {
System.out.println("DELETE"); // TODO
}
/*
* ENTF = 127 16 = Shift 17 = STRG 83 = S 66 = B 73 = I 89 = y 72 = h d=68
*
* 112 = F1 113 = F2 114 = F3 115 = F4 116 = F5 117 = F6 118 = F7 119 = F8 120 =
* F9 121 = F10 122 = F11 123 = F12
*
*/
}
// actualize on KeyRelease
public void keyReleased(KeyEvent e) {
p_display.removeAll();
SwingUtilities.updateComponentTreeUI(p_display);
if (ep_editor.getText().length() == 0)
;
else
c.load();
}
public void keyTyped(KeyEvent e) {
}
});
pack();
p_display.setLayout(new BoxLayout(p_display, BoxLayout.Y_AXIS));
// center it
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - getWidth()) / 2);
int y = (int) ((dimension.getHeight() - getHeight()) / 2);
setLocation(x, y);
setVisible(true);
// XXX hier vermute ich das Problem
getContentPane().add(scroll);
scroll.setBounds(p_display.getBounds());
//the buttons are for test reason. Other component will go here later. Supposed to be a JPanel with stuff on it.
for (int i = 0; i < 20; i++) {
p_display.add(new JButton("Hello-" + i));
}
p_display.validate();
}
private void b_yellowActionPerformed(java.awt.event.ActionEvent evt) {
c.yellow(); //make yellow better.
}
private void b_saveActionPerformed(java.awt.event.ActionEvent evt) {
c.save();
}
private void b_deleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: make delete feature
}
private void b_italicActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: italic
}
private void b_boldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:bold
}
}
Vielen Dank schonmal.
kwiff
Zuletzt bearbeitet: