My code is currently working fine, but whenever i compile, i get the Warning
C:\Users\etc\etc\FinalProject\AddItem.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.
Should I do something about it, and if so, what? if my debugging is right, it appears to be caused by the content pane properties.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.io.*;
class AddItem extends JFrame implements ActionListener
{
//text
private static JLabel TechforTeachingTitle;
private static JLabel TechforTeachingSubTitle;
//images
private static JLabel Logo;
//buttons
private JButton submitButton;
private static final int BUTTON_WIDTH = 100;
private static final int BUTTON_HEIGHT = 30;
//variables
public static void main(String[] args)
{
AddItem ProjectFrame = new AddItem();
ProjectFrame.setVisible(true); // Display the frame
//for combobox
//new AddItem().setVisible(true);
}
public AddItem ( )
{
//font properties
Font TitleFont = new Font("Serif", Font.BOLD, 80);
Font subFont = new Font("Serif", Font.BOLD, 40);
// set the frame properties
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Inventory system");
setSize(900, 700);
setLocationRelativeTo(null);
setResizable(true);
// set the content pane properties
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.decode("#FDF3E7"));
//set text properties
TechforTeachingTitle = new JLabel();
TechforTeachingTitle.setText("Tech For Teaching");
TechforTeachingTitle.setBounds(200, 30, 900, 95);
TechforTeachingTitle.setForeground(Color.decode("#8f8f8f"));
TechforTeachingTitle.setFont(TitleFont);
contentPane.add(TechforTeachingTitle);
TechforTeachingSubTitle = new JLabel();
TechforTeachingSubTitle.setText("Add an item");
TechforTeachingSubTitle.setBounds(400, 90, 900, 95); //left, top, length, height
TechforTeachingSubTitle.setForeground(Color.decode("#799177")); //7E8F7C (slightly less green)
TechforTeachingSubTitle.setFont(subFont);
contentPane.add(TechforTeachingSubTitle);
//set image properties
Logo = new JLabel(new ImageIcon("SmallLogo.png"));
Logo.setBounds(10,20,179,178); // //left, top, width, height
contentPane.add(Logo);
Logo.setVisible(true);
Logo.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
setVisible(false);
FinalProject FP = new FinalProject();
FP.setVisible(true);
}
});
//set button properties
//for combobox
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout(null); //ONLY PROBLEM WITH THIS IS GETTING THE SCREEN TO APPEAR AT CORRECT SIZE
String[] values = {"Computer", "Monitor", "Keyboard"};
final JComboBox b = new JComboBox(values);
b.setEditable(true);
add(b);
b.setBounds(300, 300, 100, 50); //this works
add(b);
submitButton = new JButton("Submit");
submitButton.setBounds(700,600, BUTTON_WIDTH, BUTTON_HEIGHT);
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String item=b.getSelectedItem().toString();
}
});
contentPane.add(submitButton);
//pack(); //not quite sure what this is for, but it makes the window tiny, so i commented it out
setLocationRelativeTo(null); //positions window center on screen
}
public void actionPerformed(ActionEvent event) // Actions
{
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
}
}
you get the warning here I think:
final JComboBox b = new JComboBox(values);
to avoid this you have the following possibilities:
add SupressWarning above the JComboBox
#SuppressWarnings("unchecked")
final JComboBox b = new JComboBox(values);
or add SupressWarning above your method
#SuppressWarnings("rawtypes")
public AddItem ( )
or my favourite add the generic:
final JComboBox<Object> b = new JComboBox<Object>(values);
Related
I need to create a GUI that has red, green and blue radiobuttons. When a radiobutton is clicked they should cause a panel in the centre of the frame to change to the corresponding backcolour colour. Here is my code below: what am I doing wrong. Here is my code below(please help; it compiles but the frame appears tiny and displays nothing):
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RadioButtons extends JFrame implements ActionListener {
JFrame f;
JPanel panel = new JPanel();
JRadioButton rb1, rb2, rb3;
JButton b1;
JButton b2;
JButton b3;
public RadioButtons() {
f = new JFrame();
rb1 = new JRadioButton("Green");
//rb1 = new JLabel(, JLabel.RIGHT);
rb1.setBounds(100, 70, 100, 40);
rb2 = new JRadioButton("Red");
rb2.setBounds(100, 90, 100, 40);
//rb2 = new JLabel("Red", JLabel.RIGHT);
rb3 = new JRadioButton("Blue");
rb3.setBounds(100, 100, 100, 40);
//rb3 = new JLabel("Blue", JLabel.RIGHT);
ButtonGroup s1 = new ButtonGroup();
s1.add(rb1);
s1.add(rb2);
s1.add(rb3);
this.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("GUI Background Changer");
b1 = new JButton("Change background color");
b1.setBounds(200, 200, 50, 50);
b1.addActionListener(this);
b2 = new JButton("Change background color");
b2.setBounds(200, 200, 50, 50);
b2.addActionListener(this);
b3 = new JButton("Change background color");
b3.setBounds(200, 200, 50, 50);
b3.addActionListener(this);
add(rb1);
add(rb2);
add(rb3);
f.add(panel);
//x.setDefaultCloseOperation(XFrame.EXIT_ON_CLOSE);
}//constructor
public static void main(String[] args) {
RadioButtons radiobuttons = new RadioButtons();
radiobuttons.setJpanelSize(200, 200, 50, 50);//setJPanelSize(200,200,50,50);
radiobuttons.setDefaultCloseOperation(EXIT_ON_CLOSE);
}// main
public void backgroundChanged(ActionEvent e) {
Color initialcolor1 = Color.RED;
Color initialcolor2 = Color.BLUE;
Color initialcolor3 = Color.GREEN;
if (rb1.isSelected()) {
panel.setBackground(initialcolor1);
}// if
else
if (rb2.isSelected()) {
panel.setBackground(initialcolor2);
}// else if
else {
panel.setBackground(initialcolor2);
}// else
}
public void setJpanelSize(int v, int w, int x, int y) {
}// setJPanel size
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}//class
it compiles but the frame appears tiny and displays nothing):
That is probably because you forgot to pack your JFrame after adding the components. Invoke pack() for your frame after adding the components.
You will also want to:
Call setVisible(true) after all other actions.
Prevent from using null layout.
I have a JButton that I would like to keep at the very right of a JTextField, regardless of how I scale the window. I am aware of BorderLayout.EAST, but that doesn't seem to work. This is my current code (userText is my JTextField):
imageButton = new JButton("Attach an Image");
if(System.getProperty("os.name").equals("Mac OS X")){
imageButton.setLocation(455, 0);
imageButton.setSize(150, 30);
} else {
imageButton.setLocation(435, 0);
imageButton.setSize(150, 20);
}
imageButton.addActionListener(
//SOME FUNCTIONALITY CODE HERE
);
userText.add(imageButton);
I know this code is very bad. It produces this if I don't resale anything (disregard what the message is):
So this looks all fine (sorry I cropped it a bit poorly), but when I resale it...
This is obviously not good looking at all. When I chamge userText.add(imageButton) to userText.add(imageButton, BorderLayout.EAST) the button simply stays in the top left corner. When I tried adding this to the JFrame, it was just a large button to the right side of the JTextArea, so I'm not quite sure what to do?
So, how can I get the button stay at the right side of the JTextField and should I even be adding the button to the JTextField or should I be adding it to some other component?
As per request here is a simple but full example (sorry about the indentation):
public class Test extends JFrame{
private JTextField userText;
private JButton imageButton;
private JTextArea chatWindow;
public Test(){
super("Test");
userText = new JTextField();
add(userText, BorderLayout.NORTH);
imageButton = new JButton("Problem Button");
if(System.getProperty("os.name").equals("Mac OS X")){
imageButton.setLocation(455, 0);
imageButton.setSize(150, 30);
}
else{
imageButton.setLocation(435, 0);
imageButton.setSize(150, 20);
}
userText.add(imageButton);
chatWindow = new JTextArea();
setSize(600, 300);
setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
}
}
Just use a JPanel for the button. Set this panel layout to FlowLayout and set its alignment to RIGHT. Then add it to the NORTH position of your frame.
Here is a sample code:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FrameTest extends JFrame {
private JTextField userText;
private JButton imageButton;
private JTextArea chatWindow;
public FrameTest() {
super("Test");
userText = new JTextField();
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(userText, BorderLayout.CENTER);
imageButton = new JButton("Problem Button");
if (System.getProperty("os.name").equals("Mac OS X")) {
imageButton.setLocation(455, 0);
imageButton.setSize(150, 30);
}
else {
imageButton.setLocation(435, 0);
imageButton.setSize(150, 20);
}
topPanel.add(imageButton, BorderLayout.EAST);
add(topPanel, BorderLayout.NORTH);
chatWindow = new JTextArea();
setSize(600, 300);
setVisible(true);
}
public static void main(String[] args) {
FrameTest test = new FrameTest();
}
}
I'm completely new to Layout Managers, but i want to create a table. I have a window that is set.Resizable(false) and doesn't have a layout manager, with 1 image and two text items placed using coordinates. I would like to keep these items using absolute layout, while creating my table underneath (using GridLayout I assume)
If it helps, here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Dimension;
class DisplayItems extends JFrame implements ActionListener
{
//text
private static JLabel TechforTeachingTitle;
private static JLabel TechforTeachingSubTitle;
//images
private static JLabel Logo;
//buttons
private static final int BUTTON_WIDTH = 100;
private static final int BUTTON_HEIGHT = 30;
public static void main(String[] args)
{
DisplayItems ProjectFrame = new DisplayItems();
ProjectFrame.setVisible(true); // Display the frame
}
public DisplayItems( )
{
//font properties
Font TitleFont = new Font("Serif", Font.BOLD, 80);
Font subFont = new Font("Serif", Font.BOLD, 40);
// set the frame properties
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Inventory system");
setSize(900, 700);
setLocationRelativeTo(null);
setResizable(false);
// set the content pane properties
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.decode("#FDF3E7"));
//set text properties
TechforTeachingTitle = new JLabel();
TechforTeachingTitle.setText("Tech For Teaching");
TechforTeachingTitle.setBounds(200, 30, 900, 95);
TechforTeachingTitle.setForeground(Color.decode("#8f8f8f"));
TechforTeachingTitle.setFont(TitleFont);
contentPane.add(TechforTeachingTitle);
TechforTeachingSubTitle = new JLabel();
TechforTeachingSubTitle.setText("View items");
TechforTeachingSubTitle.setBounds(400, 90, 900, 95); //left, top, length, height
TechforTeachingSubTitle.setForeground(Color.decode("#799177")); //7E8F7C (slightly less green)
TechforTeachingSubTitle.setFont(subFont);
contentPane.add(TechforTeachingSubTitle);
//set image properties
Logo = new JLabel(new ImageIcon("SmallLogo.png"));
Logo.setBounds(10,20,179,178); // //left, top, width, height
contentPane.add(Logo);
Logo.setVisible(true);
// Logo.addMouseListener(new MouseAdapter()
// {
// public void mouseClicked(MouseEvent e) {
// setVisible(false);
//
// FinalProject FP = new FinalProject();
// FP.setVisible(true);
// }
// });
//set button properties
}
public void actionPerformed(ActionEvent event) // Actions
{
}
}
I am encountering problems with event handling in java.
I want to add the image1 if button 1 is pressed, image2 if button 2 is pressed, et cetera.
This is my code till now; Can anyone help? This code doesn't compile.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.ImageIcon;
public class MyPanel extends JPanel {
private JLabel jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JButton jcomp4;
private JButton jcomp5;
private JButton jcomp6;
private JButton jcomp7;
private JButton jcomp8;
private JButton jcomp9;
private ImageIcon image1;
private ImageIcon image2;
private ImageIcon image3;
private ImageIcon image4;
private ImageIcon image5;
private ImageIcon image6;
private ImageIcon image7;
private ImageIcon image8;
public MyPanel() {
//construct components
image1 = new ImageIcon(getClass().getResource("hang1.jpg"));
image2 = new ImageIcon(getClass().getResource("hang2.jpg"));
image3 = new ImageIcon(getClass().getResource("hang3.jpg"));
image4 = new ImageIcon(getClass().getResource("hang4.jpg"));
image5 = new ImageIcon(getClass().getResource("hang5.jpg"));
image6 = new ImageIcon(getClass().getResource("hang6.jpg"));
image7 = new ImageIcon(getClass().getResource("hang7.jpg"));
image8 = new ImageIcon(getClass().getResource("hang8.jpg"));
jcomp1 = new JLabel (image1);
jcomp2 = new JButton ("1");
jcomp3 = new JButton ("2");
jcomp4 = new JButton ("3");
jcomp5 = new JButton ("4");
jcomp6 = new JButton ("5");
jcomp7 = new JButton ("6");
jcomp8 = new JButton ("7");
jcomp9 = new JButton ("8");
//events
jcomp2.setActionCommand("1");
jcomp3.setActionCommand("2");
jcomp4.setActionCommand("3");
jcomp5.setActionCommand("4");
jcomp6.setActionCommand("5");
jcomp7.setActionCommand("6");
jcomp8.setActionCommand("7");
jcomp9.setActionCommand("8");
jcomp2.addActionListener(new ButtonClickListener());
jcomp3.addActionListener(new ButtonClickListener());
jcomp4.addActionListener(new ButtonClickListener());
jcomp5.addActionListener(new ButtonClickListener());
jcomp6.addActionListener(new ButtonClickListener());
jcomp7.addActionListener(new ButtonClickListener());
jcomp8.addActionListener(new ButtonClickListener());
jcomp9.addActionListener(new ButtonClickListener());
//adjust size and set layout
setPreferredSize(new Dimension(624, 537));
setLayout(null);
//add components
add(jcomp2);
add(jcomp3);
add(jcomp4);
add(jcomp5);
add(jcomp6);
add(jcomp7);
add(jcomp8);
add(jcomp9);
// set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds(15, 10, 595, 350);
jcomp2.setBounds(35, 375, 100, 25);
jcomp3.setBounds(190, 375, 100, 25);
jcomp4.setBounds(320, 375, 100, 25);
jcomp5.setBounds(465, 375, 100, 25);
jcomp6.setBounds(35, 450, 100, 25);
jcomp7.setBounds(190, 450, 100, 25);
jcomp8.setBounds(320, 450, 100, 25);
jcomp9.setBounds(465, 450, 100, 25);
}
class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("1")) {
jcomp1.set(image1);
}
}
}
public static void main (String[] args) {
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setVisible (true);
}
}
I believe you are trying to get something like this:
public class MyPanel extends JPanel {
private JLabel label;
private JButton[] buttons = new JButton[8];
private ImageIcon[] images = new ImageIcon[8];
public MyPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(2, 4, 15, 10));
for (int i = 0; i < images.length; i++) {
images[i] = new ImageIcon(getClass().getResource(i+1 + ".png"));
buttons[i] = new JButton(String.valueOf(i+1));
buttons[i].setActionCommand(String.valueOf(i+1));
buttons[i].addActionListener(new ButtonClickListener());
buttonPanel.add(buttons[i]);
}
label = new JLabel(images[0]);
setLayout(new BorderLayout());
add(label);
add(buttonPanel, BorderLayout.PAGE_END);
}
class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setIcon(images[Integer.parseInt(e.getActionCommand()) - 1]);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setVisible(true);
}
}
Notes:
Don't forget to change the image file name.
You can play with the layout manager to get what you want.
I removed setPreferredSize(new Dimension(624, 537)); because you didn't specify a resize behavior which would make this line meaningless. pack() would take care of the sizes for you.
Set the icon of the button or label. To do this, you need to create an ImageIcon, which has multiple constructors to create from a filename or a loaded image.
jcomp1.setIcon(new ImageIcon(...));
Well I am a newbie in Java and i have written this simple case converter program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caseconversion extends JFrame{
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private JButton up;
private JButton low;
public caseconversion(){
super ("Case Converter");
setLayout(new FlowLayout());
tf=new JTextField("Hello whats up Buddy !!",25);
tf.setFont(new Font("Segoe Print",Font.PLAIN,15));
add(tf);
boldbox = new JCheckBox("Bold");
italicbox = new JCheckBox("Italic");
add(boldbox);
add(italicbox);
up=new JButton("Upper Case");
low=new JButton("Lowercase");
add(up);
add(low);
HandlerClass handler = new HandlerClass();
boldbox.addItemListener(handler);
italicbox.addItemListener(handler);
up.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toUpperCase());
}
}
);
low.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toLowerCase());
}
}
);
}
private class HandlerClass implements ItemListener {
//For Checkboxes
public void itemStateChanged(ItemEvent event){
Font font= null;
if(boldbox.isSelected()&&italicbox.isSelected())
font = new Font("Segoe Print",Font.BOLD + Font.ITALIC ,15);
else if(boldbox.isSelected())
font = new Font("Segoe Print",Font.BOLD ,15);
else if(italicbox.isSelected())
font = new Font("Segoe Print",Font.ITALIC ,15);
else
font= new Font("Segoe Print",Font.PLAIN,15);
tf.setFont(font);
}
}
public static void main(String arg[]){
caseconversion go = new caseconversion();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400,250);
go.setVisible(true);
}
}
Its working fine but i want to display JTextField tf in center,boldbox&italicbok on next line in center and Similarly JButtons on 3rd line in Center Can you please tell me how to do it.
The most simple solution (with minimal changes to the original code) is probably to put each row into one JPanel (where each JPanel has a FlowLayout), and arrange these 3 panels using a GridLayout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caseconversion extends JFrame{
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private JButton up;
private JButton low;
public caseconversion(){
super ("Case Converter");
setLayout(new GridLayout(0,1));
JPanel p = null;
p = new JPanel();
tf=new JTextField("Hello whats up Buddy !!",25);
tf.setFont(new Font("Segoe Print",Font.PLAIN,15));
p.add(tf);
add(p);
p = new JPanel();
boldbox = new JCheckBox("Bold");
italicbox = new JCheckBox("Italic");
p.add(boldbox);
p.add(italicbox);
add(p);
p = new JPanel();
up=new JButton("Upper Case");
low=new JButton("Lowercase");
p.add(up);
p.add(low);
add(p);
HandlerClass handler = new HandlerClass();
boldbox.addItemListener(handler);
italicbox.addItemListener(handler);
up.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toUpperCase());
}
}
);
low.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
tf.setText(tf.getText().toLowerCase());
}
}
);
}
private class HandlerClass implements ItemListener {
//For Checkboxes
public void itemStateChanged(ItemEvent event){
Font font= null;
if(boldbox.isSelected()&&italicbox.isSelected())
font = new Font("Segoe Print",Font.BOLD + Font.ITALIC ,15);
else if(boldbox.isSelected())
font = new Font("Segoe Print",Font.BOLD ,15);
else if(italicbox.isSelected())
font = new Font("Segoe Print",Font.ITALIC ,15);
else
font= new Font("Segoe Print",Font.PLAIN,15);
tf.setFont(font);
}
}
public static void main(String arg[]){
caseconversion go = new caseconversion();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400,250);
go.setVisible(true);
}
}
Note that you also have to think about the resizing behavior. Should these 3 rows always span the full height of the frame, or should they be compactly pushed to the top border of the frame?
However, a general hint (that seems to be underrated by newbies) is nesting: There's nothing wrong with creating a new JPanel in order to achieve a particular layout. Usually, a more "clean" solution to achieve this is to group several GUI components logically (and not re-using such a helper variable like the JPanel p that I used in the example).
If you using eclipse, i know a way that helped me.
JWindowbuilder: http://www.eclipse.org/windowbuilder/
If not you should look at this too:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
greetings Visores
i changed it the way you want with the WindowBuilder, i hope you like it :D
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caseconversion extends JFrame {
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
private JButton up;
private JButton low;
private Box verticalBox;
private Box horizontalBox;
private Box horizontalBox_1;
private Box horizontalBox_2;
private Component horizontalStrut;
private Component horizontalStrut_1;
private Component verticalStrut;
public caseconversion() {
super("Case Converter");
HandlerClass handler = new HandlerClass();
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
verticalBox = Box.createVerticalBox();
getContentPane().add(verticalBox);
horizontalBox = Box.createHorizontalBox();
verticalBox.add(horizontalBox);
horizontalStrut_1 = Box.createHorizontalStrut(20);
horizontalBox.add(horizontalStrut_1);
tf = new JTextField("Hello whats up Buddy !!", 25);
tf.setMaximumSize(new Dimension(2147483647, 60));
horizontalBox.add(tf);
tf.setFont(new Font("Segoe Print", Font.PLAIN, 15));
horizontalStrut = Box.createHorizontalStrut(20);
horizontalBox.add(horizontalStrut);
horizontalBox_1 = Box.createHorizontalBox();
verticalBox.add(horizontalBox_1);
boldbox = new JCheckBox("Bold");
horizontalBox_1.add(boldbox);
italicbox = new JCheckBox("Italic");
horizontalBox_1.add(italicbox);
italicbox.addItemListener(handler);
boldbox.addItemListener(handler);
horizontalBox_2 = Box.createHorizontalBox();
verticalBox.add(horizontalBox_2);
up = new JButton("Upper Case");
horizontalBox_2.add(up);
low = new JButton("Lowercase");
horizontalBox_2.add(low);
verticalStrut = Box.createVerticalStrut(20);
verticalBox.add(verticalStrut);
low.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tf.setText(tf.getText().toLowerCase());
}
});
up.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
tf.setText(tf.getText().toUpperCase());
}
});
}
private class HandlerClass implements ItemListener {
// For Checkboxes
public void itemStateChanged(ItemEvent event) {
Font font = null;
if (boldbox.isSelected() && italicbox.isSelected())
font = new Font("Segoe Print", Font.BOLD + Font.ITALIC, 15);
else if (boldbox.isSelected())
font = new Font("Segoe Print", Font.BOLD, 15);
else if (italicbox.isSelected())
font = new Font("Segoe Print", Font.ITALIC, 15);
else
font = new Font("Segoe Print", Font.PLAIN, 15);
tf.setFont(font);
}
}
public static void main(String arg[]) {
caseconversion go = new caseconversion();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 250);
go.setVisible(true);
}
}
Well, you can add BorderLayout as follows, for the JTextField :
add(tf,BorderLayout.NORTH);
and add 2 JPanel items :-
private JPanel panel1;
private JPanel panel2;
and initialize them into constructor as follows:-
panel1 =new JPanel();
panel2 =new JPanel();
and add this code
panel1.add(boldbox);
panel1.add(italicbox);
add(panel1,BorderLayout.CENTER);
panel2.add(up);
panel2.add(low);
add(panel2,BorderLayout.SOUTH);
I hope this help you.