I have Panel that contains JLayeredPane which contains two JPanels and each of them two JLabels.
Panel panel2 = new Panel();
JLayeredPane westP = new JLayeredPane();
westP.setLayout(null);
westP.setPreferredSize(new Dimension(100,400));
westP.setBackground(Color.blue);
JPanel layerOne = new JPanel();
jl1 = new JLabel("player1: ");
layerOne.add(jl1);
layerOne.setBounds(5, 100, 50, 50);
jl1Add = new JLabel("");
layerOne.add(jl1Add);
westP.add(layerOne,0, 0);
similar for another JPanel except
westP.add(LayerTwo,0,1);
and later:
panel2.setLayout(new BorderLayout());
panel2.add(westP);
add(panel2, BorderLayout.WEST);
later when I simply test it, I get lbl1, lbl1Add, lbl2 shown on GUI but not lbl2Add?
if (activeColour==RED)
{
timesPlayedRed++;
jl1Add.setText("");
jl1Add.setText(String.valueOf((timesPlayedRed)));
activeColour=YELLOW;
}
else
{
timesPlayedYellow++;
jl2Add.setText("");
jl2Add.setText(String.valueOf((timesPlayedYellow)));
activeColour=RED;
}
I am just trying out java and netbeans.
Related
//Attributes
//Stats GUI components
JLabel hp = new JLabel();
JLabel hpPoints = new JLabel("TEST");
JLabel chakra = new JLabel();
JLabel chakraPoints = new JLabel("TEST");
JLabel ryo = new JLabel();
JLabel ryoPoints = new JLabel("TEST");
//Output & Input GUI components
JTextField input = new JTextField();
JTextArea output = new JTextArea(1000, 300);
JPanel statsPanel = new JPanel();
JPanel outputPanel = new JPanel();
JPanel inputPanel = new JPanel();
//Constructor
public Terminal() {
setTitle("Shinobi Shinso");
setSize(1000, 600);
//setResizable(false);
setLocation(400, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panneau = getContentPane();
panneau.setLayout(new GridLayout(0, 1));
statsPanel.setLayout(new GridLayout(1, 3));
//Output & input
//Add outputPanel to the panneau
panneau.add(outputPanel);
//Add output to outputPanel
outputPanel.add(output);
//Add input to outputPanel
outputPanel.add(input);
input.setColumns(98);
output.setRows(15);
output.setEditable(false);
output.setBackground(Color.BLACK);
output.setForeground(Color.WHITE);
//Add stats panel
panneau.add(statsPanel);
//Statistics
//Health
hp.setIcon(new ImageIcon(new ImageIcon("D:\\eclipse-workspace\\Shinobi Shinso\\images\\scroll-hp.png").getImage().
getScaledInstance(300, 150, Image.SCALE_DEFAULT)));
hp.setHorizontalAlignment(JLabel.CENTER);
statsPanel.add(hp);
hpPoints.setBounds(100, 25, 100, 100);
hp.add(hpPoints);
setVisible(true);
}
Here's how it appears :
I tried to use a JScrollPanel and a lot of obscure coding witchcraft to no avail. I can't seem to find a way to reduce the height of the JPanel containing the pictures.
I deleted 2 of the scrolls in the picture, but I don't think that it will change anything.
I can't seem to find a way to reduce the height of the JPanel containing the pictures.
Don't use a GridLayout as the parent layout manager. The GridLayout makes all components the same size.
I would suggest you don't change the layout manager of the content pane. Leave it as the default BorderLayout.
Then use:
panneau.add(outputPanel, BorderLayout.CENTER);
panneau.add(statsPanel, BorderLayout.PAGE_END);
Also, your creation of the JTextArea is incorrect:
JTextArea output = new JTextArea(1000, 300);
The parameters are for rows/columns, not width/height.
So you should use something like:
JTextArea output = new JTextArea(15, 40);
and a text area is usually added to a JScrollPane so scrollbars can appear when needed.
Read the Swing tutorial for Swing basics. There are section on:
Layout managers
How to Use Text Areas
that should help.
I have the following Java GUI for my project. For some reason, the middle and right panels have been put below leaving empty spaces - marked in red. Is there a way to pull the panels up to fill the spaces.
These empty spaces does not look good. I have tried to check all code but cannot pinpoint where to correct.
Thanks in advance.
public static JPanel main= new JPanel(),
left= new JPanel(),middle= new JPanel(),right = new JPanel();
Container c = getContentPane();
c.setLayout(new FlowLayout()) ;
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(new JLabel("Enter word/SQL: "), "North");
pane.add(tf, "Center"); //enter keyword
JPanel second = new JPanel();
second.setLayout(new GridLayout(8, 1));
second.add(messageIDText);
//This is the start of adding panels
//create a left side panal and add the sub panels to it
JPanel left = new JPanel();
JPanel middle = new JPanel();
JPanel center = new JPanel();
JTextArea output = new JTextArea("", 60, 60);
JPanel sixth = new JPanel();
JPanel seventh = new JPanel();
JPanel eigth = new JPanel(new GridLayout(8,1));
JPanel labels7 = new JPanel(new GridLayout(5,1));
JPanel controls7 = new JPanel(new GridLayout(5,1));
JPanel tenth = new JPanel();
sixth.setLayout(new BorderLayout(5,5));
JPanel fourth = new JPanel();
fourth.add(firstButton);
sixth.add(labels, BorderLayout.WEST);
sixth.add(controls, BorderLayout.CENTER);
sixth.add(controlsButtons, BorderLayout.EAST);
labels.add(new JLabel("Proposal:"));
controls.add(proposalNumberText);
controlsButtons.add(freeQueryButton);
//add border
pane.setBorder(BorderFactory.createCompoundBorder(new
EmptyBorder(10,10,10,10), BorderFactory.createEtchedBorder()));
fourth.setBorder(BorderFactory.createCompoundBorder(new
EmptyBorder(10,10,10,10), BorderFactory.createEtchedBorder()));
//-------LEFT SIDE -- PARAMETERS
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
JLabel headerLabel,blank;
headerLabel = new JLabel("Navigate Records", JLabel.LEFT);
Font f = headerLabel.getFont();
headerLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD)); // bold
left.add(headerLabel, BorderLayout.PAGE_END);
left.add(fourth, BorderLayout.PAGE_END);
main.add(left);
//---------MIDDLE PART - Messages - tabbed pane
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
//jtp.setSize(jtp.getPreferredSize());
JPanel jpA = new JPanel(),jpB = new JPanel(),
jpC = new JPanel(),jpD = new JPanel();
JScrollPane scrollPane2 = new JScrollPane(),jp = new
JScrollPane(activeTSText), jp2 = new JScrollPane(analyseWordsText),
jp3 = new JScrollPane(markedMessageText);
JLabel labelA = new JLabel();
labelA.setText("");
jpA.add(labelA);
jpA.add(jp);
jtp.addTab("Message", jpA);
middle.add(jtp);
main.add(middle);
//----------RIGHT PART - .. //paramters and results
JPanel jj = new JPanel();
jj.setLayout(new BoxLayout(jj, BoxLayout.PAGE_AXIS));
stateJTable.setRowHeight(20); //add JTable
JScrollPane js3 =new JScrollPane(stateJTable);
js3.setVisible(true);
//JTabbedPane jtp2 = new JTabbedPane();
//JPanel StateSubstatesTabOnLeft = new JPanel(),
StateSubstates_PostProcessedTabOnLeft = new JPanel();
JPanel firstleftTabinRightSideOfFrame = new JPanel(),
secondLeftTabinRightSideOfFrame = new
JPanel(),rightTabInRightSideOfFrame = new JPanel();
jj.add(js3);
//downbelow, the state jtable is added to the panel here through the
//scrollpane which contains the jtable
right.add(jj) ; //stateJTable);// add table in panel using add() method
main.add(right);
proposalNumberText.requestFocusInWindow();
I have solved the problem by using Borderlayout and assigning the main panel to it.
The assigning the WEST, CENTER and EAST regions to it.
I have added the code below in the code above at places where the main panel is being added to.
Thank you for the advice to start debugging by using MRE approach.
BorderLayout layout = new BorderLayout();
main.setLayout(layout);
main.add(left,BorderLayout.WEST);
main.add(middle,BorderLayout.CENTER);
main.add(right,BorderLayout.EAST);
I have following source code...Can someone please give me an advice how to add jscrollpane onto jframe? I tried several time to add it to jframe but without any progress. It is not even showing.
public class Form3 {
JFrame jframe = new JFrame("Etiket print.");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
Color myBlue1Color = new Color(168, 175, 247);
Color myBlue2Color = new Color(139, 146, 255);
public Form3(){
jframe.setMinimumSize(new Dimension(1280, 1000));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setAutoscrolls(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//---------------------------------Header
panel1 = createSquareJPanel(Color.YELLOW, 600,200);
panel3 = createSquareJPanel(Color.GREEN, 400,200);
panel4 = createSquareJPanel(Color.white, 280,200);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
{
for (int i=0; i<25; i++){
JPanel harnessPanel= new JPanel();
harnessPanel.setMinimumSize(new Dimension(1280, 70));
harnessPanel.setMaximumSize(new Dimension(1280, 70));
harnessPanel.setPreferredSize(new Dimension(1280, 70));
if(i%2==0) {
harnessPanel.setBackground(myBlue1Color);
}
else {
harnessPanel.setBackground(myBlue2Color);
}
panel2.add(harnessPanel);
harnessPanel.repaint();
harnessPanel.validate();
}
panel2.repaint();
panel2.validate();
}
jframe.add(scrollFrame);
jframe.add(container);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}
private JPanel createSquareJPanel(Color color, int size1, int size2)
{
JPanel tempPanel = new JPanel();
tempPanel.setBackground(color);
tempPanel.setMinimumSize(new Dimension(size1, size2));
tempPanel.setMaximumSize(new Dimension(size1, size2));
tempPanel.setPreferredSize(new Dimension(size1, size2));
return tempPanel;
}
public static void main (String args[]){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Form3 myF=new Form3();
}
});
};
}
picture of my app:
actual state:
JFrame uses a BorderLayout by default. It's default position (if you don't specify one) is CENTER.
BorderLayout will only allow one component to occupy any of it's 5 available positions.
So when you do...
jframe.add(scrollFrame);
jframe.add(container);
It adds the scrollFrame to the center position and effectively removes it when you add container (it doesn't actually remove it, but the result is just about the same).
Try supplying a position constraint. For example...
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.SOUTH);
See How to use BorderLayout for more details
I currently have a JFrame to start fullscreen, inside this jframe i have a jpanel, this jpanel includes a vertical scrollpane. Now if i resize my jframe vertically it just kinda removes the bottom part of the jpanel. Is there any way to just shrink the jscrollpane.
im currently using flowlayout for the jframe,
Scrollbar appear automatically when the preferred size of the components added to the scroll pane area greater than the size of the scroll pane.
The FlowLayout will wrap components to a new row, but it always gives the preferred size as the size required to fit the components on a single row, so the preferred height will never change.
To solve this problem you can use the Wrap Layout which simple extend FlowLayout to recalculate the preferred size when wrapping occurs.
The JPanel consists of 3 other panels, a top panel, a scrollpane in the middle and a botpanel. The top and bot panel are just button and checkboxes and stuff
private void initPane() {
createFolderCompPanel();
createBotPanel();
createTopPanel();
createScrollPane();
createTotalPanel();
add(totalPanel);
}
private void createFolderCompPanel() {
//Create folderCompPanel
folderCompPanel = new JPanel();
folderCompPanel.setLayout(new BoxLayout(folderCompPanel, BoxLayout.Y_AXIS));
folderCompPanel.add(Box.createVerticalGlue());
}
private void createTotalPanel() {
//Create TotalPanel
totalPanel = new JPanel();
totalPanel.setLayout(new BoxLayout(totalPanel, BoxLayout.Y_AXIS));
totalPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
totalPanel.add(topPanel);
totalPanel.add(scrollPane);
totalPanel.add(botPanel);
}
private void createScrollPane() {
//Create ScrollPane
scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportBorder(BorderFactory.createEtchedBorder());
scrollPane.getVerticalScrollBar().setUnitIncrement(6);
}
private void createBotPanel() {
//Create BotPanel
botPanel = new JPanel();
botPanel.setLayout(new BoxLayout(botPanel, BoxLayout.X_AXIS));
//AddButton
addButton = new JButton("Add");
addButton.setEnabled(false);
addButton.addActionListener(this);
//SaveButton
saveButton = new JButton("Save");
saveButton.setEnabled(false);
saveButton.addActionListener(this);
//CancelButton
cancelButton = new JButton("Cancel");
cancelButton.setEnabled(false);
cancelButton.addActionListener(this);
lblTotalLength = new JLabel("Total Length: " + totalLength);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(addButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(lblTotalLength);
botPanel.add(Box.createHorizontalGlue());
botPanel.add(saveButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(cancelButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
private void createTopPanel() {
//Create TopPanel
topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
//create deletedisplay button
deleteDisplayButton = new JButton("Delete Display");
deleteDisplayButton.addActionListener(this);
deleteDisplayButton.setEnabled(false);
//create displaybox
displayBox = new JComboBox();
displayBox.addActionListener(this);
displayBox.addItem("<None>");
for (String s : connect.getAllDisplays()) {
displayBox.addItem(s);
}
displayBox.setMaximumSize(displayBox.getPreferredSize());
//create newdisplay button
newDisplayButton = new JButton("New Display");
newDisplayButton.addActionListener(this);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
topPanel.add(displayBox);
topPanel.add(Box.createHorizontalGlue());
topPanel.add(newDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(5, 0)));
topPanel.add(deleteDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
this is the panel i add to the jframe
public GuiConstructor(){
super(APPLICATION_NAME);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(630, 600));
setLayout(new FlowLayout(FlowLayout.LEFT));
LoopControlWindow folderSearch = new LoopControlWindow(connect, this);
add(folderSearch);
pack();
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I hava a problem with the applet when it is converted into an executable. the applet has three buttons with text as shown in pic_1 below, and when it is converted into an executable, the text of the three buttons disappears and the buttons becomes smalle as shown in pic_2 below.
I downloaded some applications that generates the executable file from a .jar files, and the result after using these applications is the same, the applet runs but with no buttons.
please provide any suggestions to solve this issue.
code_init() method_:
public void init () {
frame = new JFrame ();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
/* Erzeuge MenuBar*/
menubar = new JMenuBar();
frame.setJMenuBar(menubar);
einstellungen = new JMenu ("Einstellungen");
datei = new JMenuItem ("Datei");
datei.addActionListener(this);
communication = new JMenuItem ("Kommunikation");
communication.addActionListener(this);
simulation = new JMenuItem ("Simulation");
simulation.addActionListener(this);
menubar.add(einstellungen);
einstellungen.add(datei);
einstellungen.add(communication);
einstellungen.add(simulation);
/*Erzeuge Layout */
panel1 = new JPanel ();
top = new JPanel ();
center = new JPanel ();
down = new JPanel ();
bottom = new JPanel ();
frame.add(panel1); //general Panel
panel1.setLayout(new BorderLayout ()); //Separate areas in BorderLayout
panel1.add(top, BorderLayout.NORTH);
panel1.add(center, BorderLayout.CENTER);
panel1.add(bottom, BorderLayout.SOUTH);
Lspeed = new JLabel ("Geschwindigkeit: ");
Lspeedanzeige = new JLabel (" ");
LineBorder lBorder = new LineBorder(new Color(100, 100, 100));
Lspeedanzeige.setBorder(lBorder);
JLabel Lfueller = new JLabel (" ");
Ldrive = new JLabel ("Motordrehzahl: ");
Ldriveanzeige = new JLabel (" ");
LineBorder rBorder = new LineBorder(new Color(100, 100, 100));
Ldriveanzeige.setBorder(rBorder);
top.setLayout(new FlowLayout ()); //add FlowLayout to Panel top
top.add(Lspeed);
top.add(Lspeedanzeige);
top.add(Lfueller);
top.add(Ldrive);
top.add(Ldriveanzeige);
simulationarea = new JTextArea ("Gesendete Daten:");
JScrollPane scrollPane = new JScrollPane(simulationarea);
simulationarea.setPreferredSize(new Dimension(5, 100));
simulationarea.setLineWrap(true);
simulationarea.setWrapStyleWord(true);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(5, 100));
scrollPane.setVisible(true);
simulationarea.setEditable(true);
simulationarea.setEnabled(true);
int pos = simulationarea.getCaretPosition();
simulationarea.insert("file logs", pos+17);
center.setLayout(new BorderLayout ());
center.add(simulationarea, BorderLayout.NORTH); //add JTextfield simulationarea to Panel center
center.add(down, BorderLayout.SOUTH); //add Panel down zu Panel Center
Bplay = new JButton ("Play");
Bplay.setPreferredSize(new Dimension(60, 50));
Bplay.addActionListener(this);
Bbreak = new JButton ("Pause");
Bbreak.addActionListener(this);
Bstop = new JButton ("Stop");
Bstop.addActionListener(this);
down.setLayout(new FlowLayout ()); //add FlowLayout to Panel down
down.add(Bplay);
down.add(Bbreak);
down.add(Bstop);
statusarea = new JTextArea ("Nicht verbunden");
statusarea.setForeground(Color.RED);
statusarea.setPreferredSize(new Dimension(550, 50));
bottom.add(statusarea);
frame.setVisible(true);
System.out.println("init");
}//init
Pic_1
pic_2