This commit is contained in:
2026-03-25 21:12:52 +00:00
parent 6db103cd04
commit 0b407497ab

View File

@@ -8,7 +8,7 @@ import javax.swing.border.EmptyBorder;
public class Main extends JFrame {
// Cores do Design Moderno
// Cores do Design
private static final Color COLOR_BG_TOP = new Color(138, 180, 248);
private static final Color COLOR_BG_BOTTOM = new Color(25, 103, 210);
private static final Color COLOR_CARD = new Color(255, 255, 255, 220);
@@ -29,7 +29,7 @@ public class Main extends JFrame {
private JLabel lblResult;
private JLabel lblResultSub;
// Texto de Exemplo (Placeholder)
// Texto de Exemplo
private final String PLACEHOLDER_TEXT = "Ex: 100.00";
public Main() {
@@ -38,7 +38,7 @@ public class Main extends JFrame {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Painel Principal com Gradiente Azul
// Painel Principal
JPanel mainPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
@@ -55,7 +55,7 @@ public class Main extends JFrame {
mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
setContentPane(mainPanel);
// --- TÍTULO ---
// título
JLabel title = new JLabel("Conversor de Moedas", SwingConstants.CENTER);
title.setFont(new Font("Segoe UI", Font.BOLD, 30));
title.setForeground(Color.WHITE);
@@ -63,7 +63,7 @@ public class Main extends JFrame {
mainPanel.add(title);
mainPanel.add(Box.createVerticalStrut(20));
// --- CARTÃO 1: SELEÇÃO DE MOEDAS ---
// seleção de moedas
RoundedPanel cardMoedas = new RoundedPanel();
cardMoedas.setLayout(new BoxLayout(cardMoedas, BoxLayout.Y_AXIS));
@@ -80,7 +80,7 @@ public class Main extends JFrame {
cmbTo = new JComboBox<>(moedaDisplay);
styleComboBox(cmbTo);
cmbTo.setSelectedIndex(2); // Padrão BRL
cmbTo.setSelectedIndex(2);
JPanel pnlDe = new JPanel(new FlowLayout(FlowLayout.LEFT)); pnlDe.setOpaque(false); pnlDe.add(lblDe);
JPanel pnlPara = new JPanel(new FlowLayout(FlowLayout.LEFT)); pnlPara.setOpaque(false); pnlPara.add(lblPara);
@@ -94,7 +94,7 @@ public class Main extends JFrame {
mainPanel.add(cardMoedas);
mainPanel.add(Box.createVerticalStrut(15));
// --- CARTÃO 2: QUANTIDADE COM PLACEHOLDER ---
// Quantidade a Converter
RoundedPanel cardValor = new RoundedPanel();
cardValor.setLayout(new BoxLayout(cardValor, BoxLayout.Y_AXIS));
@@ -103,18 +103,16 @@ public class Main extends JFrame {
lblVal.setForeground(Color.DARK_GRAY);
JPanel pnlVal = new JPanel(new FlowLayout(FlowLayout.LEFT)); pnlVal.setOpaque(false); pnlVal.add(lblVal);
// Configuração do Placeholder
txtValue = new JTextField(PLACEHOLDER_TEXT);
txtValue.setForeground(Color.GRAY); // Cor de texto fantasma
txtValue.setForeground(Color.GRAY);
styleTextField(txtValue);
// Evento para limpar o placeholder quando se clica
txtValue.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (txtValue.getText().equals(PLACEHOLDER_TEXT)) {
txtValue.setText("");
txtValue.setForeground(Color.BLACK); // Volta à cor normal para o user escrever
txtValue.setForeground(Color.BLACK);
}
}
@@ -122,7 +120,7 @@ public class Main extends JFrame {
public void focusLost(FocusEvent e) {
if (txtValue.getText().isEmpty()) {
txtValue.setForeground(Color.GRAY);
txtValue.setText(PLACEHOLDER_TEXT); // Volta o texto fantasma se não escreverem nada
txtValue.setText(PLACEHOLDER_TEXT);
}
}
});
@@ -132,7 +130,7 @@ public class Main extends JFrame {
mainPanel.add(cardValor);
mainPanel.add(Box.createVerticalStrut(20));
// --- BOTÕES PRINCIPAIS ---
// Botões principais
JButton btnConvert = new RoundedButton("Converter Agora", COLOR_BTN_PRIMARY, Color.BLACK);
mainPanel.add(btnConvert);
mainPanel.add(Box.createVerticalStrut(10));
@@ -142,7 +140,7 @@ public class Main extends JFrame {
mainPanel.add(btnInvert);
mainPanel.add(Box.createVerticalStrut(20));
// --- CARTÃO 3: RESULTADO ---
//Resultado
RoundedPanel cardResult = new RoundedPanel();
cardResult.setLayout(new BoxLayout(cardResult, BoxLayout.Y_AXIS));
@@ -170,16 +168,21 @@ public class Main extends JFrame {
mainPanel.add(cardResult);
// --- LÓGICA DO BOTÃO CONVERTER ---
// Botão de Converter
btnConvert.addActionListener(e -> {
int indexFrom = cmbFrom.getSelectedIndex();
int indexTo = cmbTo.getSelectedIndex();
// AQUI ESTÁ O TEU AVISO! Se as moedas forem iguais, dispara o pop-up e para por aqui.
if (indexFrom == indexTo) {
JOptionPane.showMessageDialog(this, "Não podes escolher a mesma moeda! Altera uma delas.", "Aviso", JOptionPane.WARNING_MESSAGE);
return;
}
String input = txtValue.getText().replace(",", ".");
// Verifica se o utilizador não escreveu nada e deixou o placeholder lá
if (input.equals(PLACEHOLDER_TEXT) || input.isEmpty()) {
JOptionPane.showMessageDialog(this, "Insere um número válido, senão vai dar merda!", "Erro", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this, "Insere um número válido!", "Erro", JOptionPane.ERROR_MESSAGE);
return;
}
@@ -187,13 +190,7 @@ public class Main extends JFrame {
try {
val = Double.parseDouble(input);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Apenas números, por favor. Senão vai dar merda!", "Erro", JOptionPane.ERROR_MESSAGE);
return;
}
if (indexFrom == indexTo) {
lblResult.setText(String.format("%.2f %s", val, moedaAbrev[indexTo]));
lblResultSub.setText(moedaDisplay[indexTo]);
JOptionPane.showMessageDialog(this, "Apenas números, por favor.", "Erro", JOptionPane.ERROR_MESSAGE);
return;
}
@@ -220,7 +217,6 @@ public class Main extends JFrame {
}).start();
});
// Foca o ecrã para o placeholder funcionar logo na primeira vez
getContentPane().requestFocusInWindow();
}
@@ -253,9 +249,6 @@ public class Main extends JFrame {
else throw new Exception("Chave 'bid' não encontrada.");
}
// --- CLASSES DE ESTILO CUSTOMIZADAS ---
// Painel Arredondado
class RoundedPanel extends JPanel {
public RoundedPanel() {
setOpaque(false);
@@ -275,7 +268,6 @@ public class Main extends JFrame {
}
}
// Botão Arredondado
class RoundedButton extends JButton {
private Color bgColor;
public RoundedButton(String text, Color bg, Color fg) {