So I was trying to add elements into my list. But for some reason the words inside the list are very very small…. is there anyway to increase the font? I tried setFont, doesn’t work.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.DocumentListener;
public class sub extends JFrame implements ActionListener{
private JList list;
private JButton addButton = new JButton("Add");
private JButton delButton = new JButton("Delete");
private JTextField item = new JTextField(10);
private String [] s = new String[20];
public sub(){
list = new JList(s);
list.setLayout(new BorderLayout());
list.setFont(new Font("Courier New", Font.ITALIC, 12));
JScrollPane listScrollPane = new JScrollPane(list);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.setVisibleRowCount(5);
addButton.setEnabled(true);
addButton.addActionListener(this);
delButton.setEnabled(false);
delButton.addActionListener(this);
item.addActionListener(this);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(delButton);
panel.add(Box.createHorizontalStrut(5));
panel.add(new JSeparator(SwingConstants.VERTICAL));
panel.add(Box.createHorizontalStrut(5));
panel.add(item);
panel.add(addButton);
panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
add(listScrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
public static void main (String[] args){
sub sub = new sub();
sub.setVisible(true);
sub.setDefaultCloseOperation(EXIT_ON_CLOSE);
sub.setTitle("Bank Account");
sub.setSize( 250,250 );
sub.setLocation(600, 400);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == addButton){
System.out.println("ADD");
int index = list.getSelectedIndex();
if (index == -1) { //no selection, so insert at beginning
index = 0;
} else { //add after the selected item
index++;
}
s[index] = item.getText();
item.requestFocusInWindow();
item.setText("");
item.setFont(new Font("Courier New", Font.ITALIC, 12));
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
else if(e.getSource() == delButton){
System.out.println("DEL");
}
}
}
No comments:
Post a Comment