Я хотел поместить файл .gif в графический интерфейс, но отображать его с другими элементами. И файл .gif будет взят из проекта Java, а не из URL.
1 - В верхней части интерфейса будет список элементов, где мы можем выбрать один
2 - Центр будет анимированный GIF
3 - снизу будет отображаться элемент, выбранный из списка
Вот мой код (мне нужно 2 java-файла, первый (Interf.java) вызывает второй (Display.java)):
1 - Interf.java
public class Interface_for {
public static void main(String[] args) {
Display Fr = new Display();
}
}
2 - Display.java
ИНФОРМАЦИЯ: Обязательно создайте новую исходную папку (NEW> исходную папку) в своем Java-проекте и поместите файл .gif внутрь, чтобы он был виден как файл.
Я получаю файл gif с приведенным ниже кодом, так что я могу экспортировать его в проект jar (затем он анимируется).
URL url = getClass (). GetClassLoader (). GetResource ("fire.gif");
public class Display extends JFrame {
private JPanel container = new JPanel();
private JComboBox combo = new JComboBox();
private JLabel label = new JLabel("A list");
private JLabel label_2 = new JLabel ("Selection");
public Display(){
this.setTitle("Animation");
this.setSize(400, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setLayout(new BorderLayout());
combo.setPreferredSize(new Dimension(190, 20));
//We create te list of elements for the top of the GUI
String[] tab = {"Option 1","Option 2","Option 3","Option 4","Option 5"};
combo = new JComboBox(tab);
//Listener for the selected option
combo.addActionListener(new ItemAction());
//We add elements from the top of the interface
JPanel top = new JPanel();
top.add(label);
top.add(combo);
container.add(top, BorderLayout.NORTH);
//We add elements from the center of the interface
URL url = getClass().getClassLoader().getResource("fire.gif");
Icon icon = new ImageIcon(url);
JLabel center = new JLabel(icon);
container.add(center, BorderLayout.CENTER);
//We add elements from the bottom of the interface
JPanel down = new JPanel();
down.add(label_2);
container.add(down,BorderLayout.SOUTH);
this.setContentPane(container);
this.setVisible(true);
this.setResizable(false);
}
class ItemAction implements ActionListener{
public void actionPerformed(ActionEvent e){
label_2.setText("Chosen option: "+combo.getSelectedItem().toString());
}
}
}